On this page
asyncio.Event
Prev: asyncio.ensure_future | Up: asyncio | Next: asyncio.EventLoop
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.Event
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import Event
Signature & Overview
asyncio.Event()
Asynchronous equivalent to threading.Event.
Methods & Attributes
Event.clear(self)— Reset the internal flag to false. Subsequently, tasks calling wait() will block until set() is called to set the internal flag to true again.Event.is_set(self)— Return True if and only if the internal flag is true.Event.set(self)— Set the internal flag to true. All tasks waiting for it to become true are awakened. Tasks that call wait() once the flag is true will not block at all.Event.wait(self)— Block until the internal flag is true.
Usage
import asyncio
async def main():
ev = asyncio.Event()
async def trigger():
await asyncio.sleep(0)
ev.set()
t = asyncio.create_task(trigger())
await ev.wait()
await t
assert ev.is_set()
asyncio.run(main())
Gotchas
- Not thread-safe: Only call
ev.set()from the event loop's thread (from another thread, useloop.call_soon_threadsafe(ev.set)).
REPL Inspect
from asyncio import Event
help(Event)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/ensure_future Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/EventLoop Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib