← index #5843Issue #6893
Duplicate · high · value 0.388
QUERY · ISSUE

extmod/uasyncio: Loop exits if only Tasks awaiting Events are added

openby kevinkk525opened 2020-03-30updated 2021-02-18
extmod

If only Tasks are added to the Loop that are waiting for an Event to be set (or a Lock to be acquired but that makes no sense at all), the loop exits because those Tasks get removed from the main queue and put onto the waiting queue of the Event.
Use-case: Event is waiting to be set from an ISR. Will probably only ever be used without other Tasks running in minimal test-cases but would then be a problematic bug.

Expected behaviour: Loop continues to run. This needs #5795 (Events from ISR). Otherwise this problem will of course never occur.

>>> import uasyncio as asyncio
>>> ev=asyncio.Event()
>>> async def wait():
...     print("started waiting")
...     await ev.wait()
...     print("got event")
...
...
...
>>> asyncio.run(wait())
started waiting
>>>
CANDIDATE · ISSUE

uasyncio Event unexpected behaviour with only one task

closedby peterhinchopened 2021-02-13updated 2024-09-13

[EDIT] Apologies, @kevinkk525 has pointed out that this is a duplcate of https://github.com/micropython/micropython/issues/5843

This prints "start" then runs to completion where I would expect it to wait indefinitely on the Event:

import uasyncio as asyncio

evt = asyncio.Event()

async def main_loop():
    while True:
        print("start")
        await evt.wait()
        await asyncio.sleep(1)
        print("Got event")

asyncio.run(main_loop())

If an additional task is running the main_loop task behaves as expected, waiting forever:

import uasyncio as asyncio

evt = asyncio.Event()

async def main_loop():
    while True:
        print("start")
        await evt.wait()
        await asyncio.sleep(1)
        print("Got event")

async def main():
    asyncio.create_task(main_loop())
    while True:
        await asyncio.sleep(1)

asyncio.run(main())

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied