On this page
asyncio.Condition
Prev: asyncio.capture_call_graph | Up: asyncio | Next: asyncio.create_eager_task_factory
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.Condition
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import Condition
Signature & Overview
asyncio.Condition(lock=None)
Asynchronous equivalent to threading.Condition.
Methods & Attributes
Condition.notify(self, n=1)— By default, wake up one task waiting on this condition, if any.Condition.notify_all(self)— Wake up all tasks waiting on this condition. This method acts like notify(), but wakes up all waiting tasks instead of one. If the calling task has not acquired the lock when this method is called,Condition.wait(self)— Wait until notified.Condition.wait_for(self, predicate)— Wait until a predicate becomes true.
Usage
import asyncio
async def main():
cond = asyncio.Condition()
ready = False
async def setter():
nonlocal ready
async with cond:
ready = True
cond.notify_all()
t = asyncio.create_task(setter())
async with cond:
await cond.wait_for(lambda: ready)
await t
assert ready is True
asyncio.run(main())
Gotchas
- Must hold
async with cond:beforewait()/notify(): Preferwait_for(predicate)over barewait().
REPL Inspect
from asyncio import Condition
help(Condition)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/capture_call_graph Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/create_eager_task_factory 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