Python 3.4+Async programmingterm
asyncio.Event
synchronization
A flag for signalling between tasks: some wait with await event.wait(), another calls event.set(), and all waiters wake up at once.
Syntax
event = asyncio.Event(); await event.wait(); event.set(); event.clear()
After set() the flag stays raised: every later wait() returns immediately until you call clear() — an Event is a state, not a one-shot nudge. set() is a plain method, not a coroutine, so it is not awaited. Do not call set() from another thread — this is not threading.Event; use loop.call_soon_threadsafe(event.set).
Examples
import asyncio
async def waiter(name, event):
await event.wait()
print(name, 'проснулся')
async def main():
event = asyncio.Event()
tasks = [asyncio.create_task(waiter(n, event)) for n in ('a', 'b')]
await asyncio.sleep(0.01)
event.set()
await asyncio.gather(*tasks) # → a проснулся / b проснулся
print(event.is_set()) # → True — флаг остался поднят
asyncio.run(main())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy