diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-04-17 23:51:58 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-04-17 23:51:58 (GMT) |
commit | 0454af9b54c4beee27d4b119771bce3d7162c793 (patch) | |
tree | 18ad6d31cec4731853b5e54608fa039a1a8cc52e /Lib/threading.py | |
parent | 2d9cb9c1cb3a7dd2e60a323271fbfe80854a6817 (diff) | |
download | cpython-0454af9b54c4beee27d4b119771bce3d7162c793.zip cpython-0454af9b54c4beee27d4b119771bce3d7162c793.tar.gz cpython-0454af9b54c4beee27d4b119771bce3d7162c793.tar.bz2 |
Issue #850728: Add a *timeout* parameter to the `acquire()` method of
`threading.Semaphore` objects. Original patch by Torsten Landschoff.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r-- | Lib/threading.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/threading.py b/Lib/threading.py index b4d07fe..2ca224e 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -290,8 +290,11 @@ class _Semaphore(_Verbose): self._cond = Condition(Lock()) self._value = value - def acquire(self, blocking=True): + def acquire(self, blocking=True, timeout=None): + if not blocking and timeout is not None: + raise ValueError("can't specify timeout for non-blocking acquire") rc = False + endtime = None self._cond.acquire() while self._value == 0: if not blocking: @@ -299,7 +302,14 @@ class _Semaphore(_Verbose): if __debug__: self._note("%s.acquire(%s): blocked waiting, value=%s", self, blocking, self._value) - self._cond.wait() + if timeout is not None: + if endtime is None: + endtime = _time() + timeout + else: + timeout = endtime - _time() + if timeout <= 0: + break + self._cond.wait(timeout) else: self._value = self._value - 1 if __debug__: |