Commit 962e015f authored by Jason Madden's avatar Jason Madden

Merge pull request #772 from gevent/feature-503-group-join-return

Return whether the pool became empty in join.
parents 71212515 813fd8bf
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
- Nested callbacks that set and clear an Event no longer cause - Nested callbacks that set and clear an Event no longer cause
``wait`` to return prematurely. Reported in :issue:`771` by Sergey ``wait`` to return prematurely. Reported in :issue:`771` by Sergey
Vasilyev. Vasilyev.
- :class:`~.Group` and :class:`~.Pool` now return whether
:meth:`~.Group.join` returned with an empty group. Suggested by Filippo Sironi in
:pr:`503`.
1.1.0 (Mar 5, 2016) 1.1.0 (Mar 5, 2016)
=================== ===================
......
...@@ -540,17 +540,25 @@ class Group(GroupMappingMixin): ...@@ -540,17 +540,25 @@ class Group(GroupMappingMixin):
one gets re-raised is not determined. Only greenlets currently one gets re-raised is not determined. Only greenlets currently
in the group when this method is called are guaranteed to in the group when this method is called are guaranteed to
be checked for exceptions. be checked for exceptions.
:return bool: A value indicating whether this group became empty.
If the timeout is specified and the group did not become empty
during that timeout, then this will be a false value. Otherwise
it will be a true value.
.. versionchanged:: 1.2a1
Add the return value.
""" """
if raise_error: greenlets = list(self.greenlets) if raise_error else ()
greenlets = self.greenlets.copy() result = self._empty_event.wait(timeout=timeout)
self._empty_event.wait(timeout=timeout)
for greenlet in greenlets: for greenlet in greenlets:
if greenlet.exception is not None: if greenlet.exception is not None:
if hasattr(greenlet, '_raise_exception'): if hasattr(greenlet, '_raise_exception'):
greenlet._raise_exception() greenlet._raise_exception()
raise greenlet.exception raise greenlet.exception
else:
self._empty_event.wait(timeout=timeout) return result
def kill(self, exception=GreenletExit, block=True, timeout=None): def kill(self, exception=GreenletExit, block=True, timeout=None):
""" """
......
...@@ -465,7 +465,8 @@ class TestJoinEmpty(greentest.TestCase): ...@@ -465,7 +465,8 @@ class TestJoinEmpty(greentest.TestCase):
def test(self): def test(self):
p = pool.Pool() p = pool.Pool()
p.join() res = p.join()
self.assertTrue(res, "empty should return true")
class TestSpawn(greentest.TestCase): class TestSpawn(greentest.TestCase):
...@@ -481,6 +482,16 @@ class TestSpawn(greentest.TestCase): ...@@ -481,6 +482,16 @@ class TestSpawn(greentest.TestCase):
gevent.sleep(0.19 if not greentest.RUNNING_ON_APPVEYOR else 0.5) gevent.sleep(0.19 if not greentest.RUNNING_ON_APPVEYOR else 0.5)
self.assertEqual(len(p), 0) self.assertEqual(len(p), 0)
def testSpawnAndWait(self):
p = pool.Pool(1)
self.assertEqual(len(p), 0)
p.spawn(gevent.sleep, 0.1)
self.assertEqual(len(p), 1)
res = p.join(0.01)
self.assertFalse(res, "waiting on a full pool should return false")
res = p.join()
self.assertTrue(res, "waiting to finish should be true")
self.assertEqual(len(p), 0)
def error_iter(): def error_iter():
yield 1 yield 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment