@@ -345,16 +345,19 @@ BoundedSemaphore
345
345
Barrier
346
346
=======
347
347
348
- .. versionadded :: 3.11
348
+ .. class :: Barrier(parties, action=None)
349
349
350
350
A barrier object. Not thread-safe.
351
351
352
- A barrier is a simple synchronization primitive that allows to block until a
353
- certain number of tasks are waiting on it.
352
+ A barrier is a simple synchronization primitive that allows to block until
353
+ * parties * number of tasks are waiting on it.
354
354
Tasks can wait on the :meth: `~Barrier.wait ` method and would be blocked until
355
355
the specified number of tasks end up waiting on :meth: `~Barrier.wait `.
356
356
At that point all of the waiting tasks would unblock simultaneously.
357
357
358
+ :keyword: `async with ` can be used as an alternative to awaiting on
359
+ :meth: `~Barrier.wait `.
360
+
358
361
The barrier can be reused any number of times.
359
362
360
363
.. _asyncio_example_barrier :
@@ -363,7 +366,7 @@ Barrier
363
366
364
367
async def example_barrier():
365
368
# barrier with 3 parties
366
- b = asyncio.Barrier(3, done )
369
+ b = asyncio.Barrier(3)
367
370
368
371
# create 2 new waiting tasks
369
372
asyncio.create_task(b.wait())
@@ -372,10 +375,10 @@ Barrier
372
375
await asyncio.sleep(0)
373
376
print(b)
374
377
375
- # Now, current task waits
376
- async with b as pos:
377
- print(b)
378
- print("barrier passed")
378
+ # The third .wait() call passes the barrier
379
+ awaut b.wait()
380
+ print(b)
381
+ print("barrier passed")
379
382
380
383
await asyncio.sleep(0)
381
384
print(b)
@@ -384,17 +387,12 @@ Barrier
384
387
385
388
Result of this example is::
386
389
387
- <asyncio.locks.Barrier object at 0x105d70100 [filling, waiters:2/3]>
388
- <asyncio.locks.Barrier object at 0x10146c6a0 [draining, waiters:0/3]>
390
+ <asyncio.locks.Barrier object at 0x... [filling, waiters:2/3]>
391
+ <asyncio.locks.Barrier object at 0x... [draining, waiters:0/3]>
389
392
barrier passed
390
- <asyncio.locks.Barrier object at 0x105d70100 [filling, waiters:0/3]>
393
+ <asyncio.locks.Barrier object at 0x... [filling, waiters:0/3]>
391
394
392
- The example also demonstrates using ``async with `` as an alternative to awaiting
393
- on ``barrier.wait() ``.
394
-
395
- .. class :: Barrier(parties, action=None)
396
-
397
- Create a barrier object for *parties * number of tasks.
395
+ .. versionadded :: 3.11
398
396
399
397
.. coroutinemethod :: wait()
400
398
0 commit comments