8000 Change bounded semaphore into a subclass, like threading.[Bounded]Sem… · Python-Repository-Hub/asyncio@94197b0 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 94197b0

Browse files
committed
Change bounded semaphore into a subclass, like threading.[Bounded]Semaphore.
1 parent f4f0267 commit 94197b0

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

asyncio/locks.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,15 @@ class Semaphore:
336336
337337
Semaphores also support the context manager protocol.
338338
339-
The first optional argument gives the initial value for the internal
339+
The optional argument gives the initial value for the internal
340340
counter; it defaults to 1. If the value given is less than 0,
341341
ValueError is raised.
342-
343-
The second optional argument determines if the semaphore can be released
344-
more than initial internal counter value; it defaults to False. If the
345-
value given is True and number of release() is more than number of
346-
successful acquire() calls ValueError is raised.
347342
"""
348343

349-
def __init__(self, value=1, bound=False, *, loop=None):
344+
def __init__(self, value=1, *, loop=None):
350345
if value < 0:
351346
raise ValueError("Semaphore initial value must be >= 0")
352347
self._value = value
353-
self._bound = bound
354-
self._bound_value = value
355348
self._waiters = collections.deque()
356349
self._locked = (value == 0)
357350
if loop is not None:
@@ -402,17 +395,9 @@ def release(self):
402395
"""Release a semaphore, incrementing the internal counter by one.
403396
When it was zero on entry and another coroutine is waiting for it to
404397
become larger than zero again, wake up that coroutine.
405-
406-
If Semaphore is created with "bound" parameter equals true, then
407-
release() method checks to make sure its current value doesn't exceed
408-
its initial value. If it does, ValueError is raised.
409398
"""
410-
if self._bound and self._value >= self._bound_value:
411-
raise ValueError('Semaphore released too many times')
412-
413399
self._value += 1
414400
self._locked = False
415-
416401
for waiter in self._waiters:
417402
if not waiter.done():
418403
waiter.set_result(True)
@@ -429,3 +414,20 @@ def __exit__(self, *args):
429414
def __iter__(self):
430415
yield from self.acquire()
431416
return self
417+
418+
419+
class BoundedSemaphore(Semaphore):
420+
"""A bounded semaphore implementation.
421+
422+
This raises ValueError in release() if it would increase the value
423+
above the initial value.
424+
"""
425+
426+
def __init__(self, value=1, *, loop=None):
427+
self._bound_value = value
428+
super().__init__(value, loop=loop)
429+
430+
def release(self):
431+
if self._value >= self._bound_value:
432+
raise ValueError('BoundedSemaphore released too many times')
433+
super().release()

tests/test_locks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ def test_acquire_cancel(self):
805805
self.assertFalse(sem._waiters)
806806

807807
def test_release_not_acquired(self):
808-
sem = locks.Semaphore(bound=True, loop=self.loop)
808+
sem = locks.BoundedSemaphore(loop=self.loop)
809809

810810
self.assertRaises(ValueError, sem.release)
811811

0 commit comments

Comments
 (0)
0