diff options
author | Charles-François Natali <neologix@free.fr> | 2012-04-17 16:45:57 (GMT) |
---|---|---|
committer | Charles-François Natali <neologix@free.fr> | 2012-04-17 16:45:57 (GMT) |
commit | c8ce715a82fd8034ef1d809b262346c15f2490c4 (patch) | |
tree | 125522b5a5daa37ba405f81902aeebe8d91b6c7f /Lib/multiprocessing/synchronize.py | |
parent | a3f4457b172a165fafa65d67e0293f89dbba06b7 (diff) | |
download | cpython-c8ce715a82fd8034ef1d809b262346c15f2490c4.zip cpython-c8ce715a82fd8034ef1d809b262346c15f2490c4.tar.gz cpython-c8ce715a82fd8034ef1d809b262346c15f2490c4.tar.bz2 |
Issue #14087: multiprocessing: add Condition.wait_for(). Patch by sbt.
Diffstat (limited to 'Lib/multiprocessing/synchronize.py')
-rw-r--r-- | Lib/multiprocessing/synchronize.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index e35bbff..532ac5c 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -43,6 +43,7 @@ import _multiprocessing from multiprocessing.process import current_process from multiprocessing.util import register_after_fork, debug from multiprocessing.forking import assert_spawning, Popen +from time import time as _time # Try to import the mp.synchronize module cleanly, if it fails # raise ImportError for platforms lacking a working sem_open implementation. @@ -290,6 +291,24 @@ class Condition(object): while self._wait_semaphore.acquire(False): pass + def wait_for(self, predicate, timeout=None): + result = predicate() + if result: + return result + if timeout is not None: + endtime = _time() + timeout + else: + endtime = None + waittime = None + while not result: + if endtime is not None: + waittime = endtime - _time() + if waittime <= 0: + break + self.wait(waittime) + result = predicate() + return result + # # Event # |