# HG changeset patch # Parent da541f3c2f237a47c95bc294f0cd303e8dcc5db2 diff -r da541f3c2f23 Lib/multiprocessing/managers.py --- a/Lib/multiprocessing/managers.py Thu Feb 23 02:41:22 2012 +0000 +++ b/Lib/multiprocessing/managers.py Thu Feb 23 13:36:57 2012 +0000 @@ -48,6 +48,7 @@ from multiprocessing import Process, current_process, active_children, Pool, util, connection from multiprocessing.process import AuthenticationString from multiprocessing.forking import exit, Popen, ForkingPickler +from time import time as _time # # Register some things for pickling @@ -996,6 +997,22 @@ return self._callmethod('notify') def notify_all(self): return self._callmethod('notify_all') + def wait_for(self, predicate, timeout=None): + endtime = None + waittime = timeout + result = predicate() + while not result: + if waittime is not None: + if endtime is None: + endtime = _time() + waittime + else: + waittime = endtime - _time() + if waittime <= 0: + break + self.wait(waittime) + result = predicate() + return result + class EventProxy(BaseProxy): _exposed_ = ('is_set', 'set', 'clear', 'wait') diff -r da541f3c2f23 Lib/multiprocessing/synchronize.py --- a/Lib/multiprocessing/synchronize.py Thu Feb 23 02:41:22 2012 +0000 +++ b/Lib/multiprocessing/synchronize.py Thu Feb 23 13:36:57 2012 +0000 @@ -43,6 +43,7 @@ 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. @@ -240,7 +241,7 @@ try: # wait for notification or timeout - ret = self._wait_semaphore.acquire(True, timeout) + return self._wait_semaphore.acquire(True, timeout) finally: # indicate that this thread has woken self._woken_count.release() @@ -248,7 +249,6 @@ # reacquire lock for i in range(count): self._lock.acquire() - return ret def notify(self): assert self._lock._semlock._is_mine(), 'lock is not owned' @@ -290,6 +290,22 @@ while self._wait_semaphore.acquire(False): pass + def wait_for(self, predicate, timeout=None): + endtime = None + waittime = timeout + result = predicate() + while not result: + if waittime is not None: + if endtime is None: + endtime = _time() + waittime + else: + waittime = endtime - _time() + if waittime <= 0: + break + self.wait(waittime) + result = predicate() + return result + # # Event # diff -r da541f3c2f23 Lib/test/test_multiprocessing.py --- a/Lib/test/test_multiprocessing.py Thu Feb 23 02:41:22 2012 +0000 +++ b/Lib/test/test_multiprocessing.py Thu Feb 23 13:36:57 2012 +0000 @@ -887,6 +887,70 @@ self.assertEqual(res, False) self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1) + @classmethod + def _test_waitfor_f(cls, cond, state): + with cond: + state.value = 0 + cond.notify() + result = cond.wait_for(lambda : state.value==4) + if not result or state.value != 4: + sys.exit(1) + + def test_waitfor(self): + # based on test in test/lock_tests.py + cond = self.Condition() + state = self.Value('i', -1) + + p = self.Process(target=self._test_waitfor_f, args=(cond, state)) + p.daemon = True + p.start() + + with cond: + result = cond.wait_for(lambda : state.value==0) + self.assertTrue(result) + self.assertEqual(state.value, 0) + for i in range(4): + time.sleep(0.01) + with cond: + state.value += 1 + cond.notify() + + p.join(5) + self.assertFalse(p.is_alive()) + self.assertEqual(p.exitcode, 0) + + @classmethod + def _test_waitfor_timeout_f(cls, cond, state, success): + with cond: + expected = 0.1 + dt = time.time() + result = cond.wait_for(lambda : state.value==4, timeout=expected) + dt = time.time() - dt + # borrow logic in assertTimeout() from test/lock_tests.py + if not result and expected * 0.6 < dt < expected * 10.0: + success.value = True + + def test_waitfor_timeout(self): + # based on test in test/lock_tests.py + cond = self.Condition() + state = self.Value('i', 0) + success = self.Value('i', False) + + p = self.Process(target=self._test_waitfor_timeout_f, + args=(cond, state, success)) + p.daemon = True + p.start() + + # Only increment 3 times, so state == 4 is never reached. + for i in range(3): + time.sleep(0.01) + with cond: + state.value += 1 + cond.notify() + + p.join(5) + self.assertTrue(success.value) + class _TestEvent(BaseTestCase):