On this page
asyncio.Barrier
Prev: asyncio.as_completed | Up: asyncio | Next: asyncio.BaseEventLoop
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.Barrier
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import Barrier
Signature & Overview
asyncio.Barrier(parties)
Asyncio equivalent to threading.Barrier Implements a Barrier primitive.
Methods & Attributes
Barrier.abort(self)— Place the barrier into a 'broken' state.Barrier.broken(...)— Return True if the barrier is in a broken state.Barrier.n_waiting(...)— Return the number of tasks currently waiting at the barrier.Barrier.parties(...)— Return the number of tasks required to trip the barrier.Barrier.reset(self)— Reset the barrier to the initial state.Barrier.wait(self)— Wait for the barrier.
Usage
import asyncio
async def main():
barrier = asyncio.Barrier(2)
async def worker():
await barrier.wait()
return "done"
res = await asyncio.gather(worker(), worker())
assert res == ["done", "done"]
asyncio.run(main())
Gotchas
- Requires Python 3.11+: Wrap
await barrier.wait()withasyncio.timeout(...)so a failing task doesn't hang the remaining waiters.
REPL Inspect
from asyncio import Barrier
help(Barrier)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop 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/BaseProtocol shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseTransport shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BoundedSemaphore shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BrokenBarrierError shared tags: #asyncio, #stdlib