From: Jesse Noller Date: Wed, 1 Apr 2009 03:45:50 +0000 (+0000) Subject: Fix multiprocessing.event to match the new threading.Event API X-Git-Tag: v2.7a1~1646 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=02cb0eb231fc01e2a50cbe14f6da7a8df52959ef;p=python Fix multiprocessing.event to match the new threading.Event API --- diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 3d66d14742..02f6ac1c47 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -836,6 +836,12 @@ object -- see :ref:`multiprocessing-managers`. .. class:: Event() A clone of :class:`threading.Event`. + This method returns the state of the internal semaphore on exit, so it + will always return ``True`` except if a timeout is given and the operation + times out. + + .. versionchanged:: 2.7 + Previously, the method always returned ``None``. .. class:: Lock() diff --git a/Lib/multiprocessing/synchronize.py b/Lib/multiprocessing/synchronize.py index dacf45acca..716f3c7003 100644 --- a/Lib/multiprocessing/synchronize.py +++ b/Lib/multiprocessing/synchronize.py @@ -301,5 +301,10 @@ class Event(object): self._flag.release() else: self._cond.wait(timeout) + + if self._flag.acquire(False): + self._flag.release() + return True + return False finally: self._cond.release() diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index a446ac304d..8ef2e2fc7b 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -749,20 +749,22 @@ class _TestEvent(BaseTestCase): # Removed temporaily, due to API shear, this does not # work with threading._Event objects. is_set == isSet - #self.assertEqual(event.is_set(), False) + self.assertEqual(event.is_set(), False) - self.assertEqual(wait(0.0), None) + # Removed, threading.Event.wait() will return the value of the __flag + # instead of None. API Shear with the semaphore backed mp.Event + self.assertEqual(wait(0.0), False) self.assertTimingAlmostEqual(wait.elapsed, 0.0) - self.assertEqual(wait(TIMEOUT1), None) + self.assertEqual(wait(TIMEOUT1), False) self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1) event.set() # See note above on the API differences - # self.assertEqual(event.is_set(), True) - self.assertEqual(wait(), None) + self.assertEqual(event.is_set(), True) + self.assertEqual(wait(), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) - self.assertEqual(wait(TIMEOUT1), None) + self.assertEqual(wait(TIMEOUT1), True) self.assertTimingAlmostEqual(wait.elapsed, 0.0) # self.assertEqual(event.is_set(), True) @@ -771,7 +773,7 @@ class _TestEvent(BaseTestCase): #self.assertEqual(event.is_set(), False) self.Process(target=self._test_event, args=(event,)).start() - self.assertEqual(wait(), None) + self.assertEqual(wait(), True) # #