git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

asyncio event guarantees to notify all waiting?


Hello,

I'm wondering if set'ing an asyncio.Event guarantees to notify all tasks that are waiting for the event ?

Thus even if I `set()` the event and directly `clear()` the event, considering that both thus instructions are no co-routines and thus will not return control to the event-loop, will the wait'ers be notified?

I guess so because if I add a 'clear()' directly after the `set` in following example, the waiting task is still notified. However this guarantee is not in the documentation:

```
async def waiter(event):
    print('waiting for it ...')
    await event.wait()
    print('... got it!')

async def main():
    # Create an Event object.
    event = asyncio.Event()

    # Spawn a Task to wait until 'event' is set.
    waiter_task = asyncio.create_task(waiter(event))

    # Sleep for 1 second and set the event.
    await asyncio.sleep(1)
    event.set()
    event.clear()  # event directly cleared after being set

    # Wait until the waiter task is finished.
    await waiter_task

asyncio.run(main())
```