This commit is contained in:
Nadeshiko Manju 2026-02-05 17:04:14 +09:00 committed by GitHub
commit 690cd3bd35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 17 deletions

View file

@ -1072,7 +1072,7 @@ item to the buffer only needs to wake up one consumer thread.
.. versionadded:: 3.2
.. method:: notify(n=1)
.. method:: notify(n=1, timeout=None)
By default, wake up one thread waiting on this condition, if any. If the
calling thread has not acquired the lock when this method is called, a
@ -1090,7 +1090,10 @@ item to the buffer only needs to wake up one consumer thread.
call until it can reacquire the lock. Since :meth:`notify` does not
release the lock, its caller should.
.. method:: notify_all()
.. versionchanged:: 3.14
The *timeout* parameter is new.
.. method:: notify_all(timeout=None)
Wake up all threads waiting on this condition. This method acts like
:meth:`notify`, but wakes up all waiting threads instead of one. If the
@ -1099,6 +1102,9 @@ item to the buffer only needs to wake up one consumer thread.
The method ``notifyAll`` is a deprecated alias for this method.
.. versionchanged:: 3.14
The *timeout* parameter is new.
.. _semaphore-objects:

View file

@ -281,7 +281,7 @@ def wait(self, timeout=None):
for i in range(count):
self._lock.acquire()
def notify(self, n=1):
def notify(self, n=1, timeout=None):
assert self._lock._semlock._is_mine(), 'lock is not owned'
assert not self._wait_semaphore.acquire(
False), ('notify: Should not have been able to acquire '
@ -289,26 +289,25 @@ def notify(self, n=1):
# to take account of timeouts since last notify*() we subtract
# woken_count from sleeping_count and rezero woken_count
while self._woken_count.acquire(False):
res = self._sleeping_count.acquire(False)
while self._woken_count.acquire(False, timeout=timeout):
res = self._sleeping_count.acquire(False, timeout=timeout)
assert res, ('notify: Bug in sleeping_count.acquire'
+ '- res should not be False')
sleepers = 0
while sleepers < n and self._sleeping_count.acquire(False):
while sleepers < n and self._sleeping_count.acquire(False, timeout=timeout):
self._wait_semaphore.release() # wake up one sleeper
sleepers += 1
if sleepers:
for i in range(sleepers):
self._woken_count.acquire() # wait for a sleeper to wake
self._woken_count.acquire(timeout=timeout) # wait for a sleeper to wake
# rezero wait_semaphore in case some timeouts just happened
while self._wait_semaphore.acquire(False):
while self._wait_semaphore.acquire(False, timeout=timeout):
pass
def notify_all(self):
self.notify(n=sys.maxsize)
def notify_all(self, timeout=None):
self.notify(n=sys.maxsize, timeout=timeout)
def wait_for(self, predicate, timeout=None):
result = predicate()

View file

@ -398,7 +398,7 @@ def wait_for(self, predicate, timeout=None):
result = predicate()
return result
def notify(self, n=1):
def notify(self, n=1, timeout=None):
"""Wake up one or more threads waiting on this condition, if any.
If the calling thread has not acquired the lock when this method is
@ -428,14 +428,14 @@ def notify(self, n=1):
except ValueError:
pass
def notify_all(self):
def notify_all(self, timeout=None):
"""Wake up all threads waiting on this condition.
If the calling thread has not acquired the lock when this method
is called, a RuntimeError is raised.
"""
self.notify(len(self._waiters))
self.notify(len(self._waiters), timeout=timeout)
def notifyAll(self):
"""Wake up all threads waiting on this condition.
@ -727,7 +727,7 @@ def wait(self, timeout=None):
try:
if index + 1 == self._parties:
# We release the barrier
self._release()
self._release(timeout=timeout)
else:
# We wait until someone releases us
self._wait(timeout)
@ -750,13 +750,13 @@ def _enter(self):
# Optionally run the 'action' and release the threads waiting
# in the barrier.
def _release(self):
def _release(self, timeout=None):
try:
if self._action:
self._action()
# enter draining state
self._state = 1
self._cond.notify_all()
self._cond.notify_all(timeout=timeout)
except:
#an exception during the _action handler. Break and reraise
self._break()

View file

@ -0,0 +1,2 @@
Add timeout argument for notify_all/notify method in Condition class in
threading/multiprocessing module