8000 bpo-43352: Add a Barrier object to asyncio synchronized primitives (G… · python/cpython@90e5d04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 90e5d04

Browse files
committed
bpo-43352: Add a Barrier object to asyncio synchronized primitives (GH-24903)
Remove `n_blocking` attribut Restore `broken` instead of `_is_broken()` Rename all `_is_xxxx' methods to `_xxxx' where `xxxx' in draining, resetting, filling
1 parent a2467f5 commit 90e5d04

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

Lib/asyncio/locks.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -492,15 +492,15 @@ async def _block(self):
492492
# It is draining or resetting, wait until done
493493
# unless a CancelledError occurs
494494
try:
495-
await self._cond.wait_for(lambda: not (self._is_draining() or
496-
self._is_resetting()))
495+
await self._cond.wait_for(lambda: not (self._draining() or
496+
self._resetting()))
497497
except exceptions.CancelledError:
498498
self._count_block -= 1
499499
raise
500500
self._count_block -= 1
501501

502502
# see if the barrier is in a broken state
503-
if self._is_broken():
503+
if self.broken:
504504
raise BrokenBarrierError
505505

506506
# Optionally run the 'action' and release the tasks waiting
@@ -528,17 +528,17 @@ async def _wait(self):
528528
"""
529529
# wait for end of filling
530530
# unless a CancelledError occurs
531-
await self._cond.wait_for(lambda: not self._is_filling())
531+
await self._cond.wait_for(lambda: not self._filling())
532532

533-
if self._is_broken() or self._is_resetting():
533+
if self. 8000 broken or self._resetting():
534534
raise BrokenBarrierError
535535

536536
def _exit(self):
537537
"""If we are the last tasks to exit the barrier, signal any tasks
538538
waiting for the barrier to drain.
539539
"""
540540
if self._count == 0:
541-
if self._is_resetting() or self._is_draining():
541+
if self._resetting() or self._draining():
542542
self._set_filling()
543543
self._cond.notify_all()
544544

@@ -549,8 +549,8 @@ async def reset(self):
549549
"""
550550
async with self._cond:
551551
if self._count > 0:
552-
if not self._is_resetting():# self._is_filling()
553-
# or self._is_draining()
552+
if not self._resetting():# self._filling()
553+
# or self._draining()
554554
# or self.is_broken()
555555
#reset the barrier, waking up tasks
556556
self._set_resetting()
@@ -580,16 +580,26 @@ def parties(self):
580580
@property
581581
def n_waiting(self):
582582
"""Return the number of tasks currently waiting at the barrier."""
583-
if self._is_filling():
583+
if self._filling():
584584
return self._count
585585
return 0
586586

587587
@property
588-
def n_blocking(self):
589-
"""Return the number of tasks currently blocking at the barrier."""
590-
if self._is_draining():
591-
return self._count_block
592-
return 0
588+
def broken(self):
589+
"""Return True if the barrier is in a broken state."""
590+
return self._state == -2
591+
592+
def _draining(self):
593+
"""Return True if the barrier is draining."""
594+
return self._state == 1
595+
596+
def _filling(self):
597+
"""Return True if the barrier is filling."""
598+
return self._state == 0
599+
600+
def _resetting(self):
601+
"""Return True if the barrier is resetting."""
602+
return self._state == -1
593603

594604
def _set_broken(self):
595605
"""Set state to broken."""
@@ -607,22 +617,6 @@ def _set_resetting(self):
607617
"""Set state to resetting."""
608618
self._state = -1
609619

610-
def _is_broken(self):
611-
"""Return True if the barrier is in a broken state."""
612-
return self._state == -2
613-
614-
def _is_draining(self):
615-
"""Return True if the barrier is draining."""
616-
return self._state == 1
617-
618-
def _is_filling(self):
619-
"""Return True if the barrier is filling."""
620-
return self._state == 0
621-
622-
def _is_resetting(self):
623-
"""Return True if the barrier is resetting."""
624-
return self._state == -1
625-
626620

627621
# exception raised by the Barrier class
628622
class BrokenBarrierError(RuntimeError):

0 commit comments

Comments
 (0)
0