Commit 813fd8bf authored by Jason Madden's avatar Jason Madden

Return whether the pool became empty in join.

Useful with a timeout argument.

Fixes #503.
parent 71212515
......@@ -19,6 +19,9 @@
- Nested callbacks that set and clear an Event no longer cause
``wait`` to return prematurely. Reported in :issue:`771` by Sergey
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)
===================
......
......@@ -540,17 +540,25 @@ class Group(GroupMappingMixin):
one gets re-raised is not determined. Only greenlets currently
in the group when this method is called are guaranteed to
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 = self.greenlets.copy()
self._empty_event.wait(timeout=timeout)
for greenlet in greenlets:
if greenlet.exception is not None:
if hasattr(greenlet, '_raise_exception'):
greenlet._raise_exception()
raise greenlet.exception
else:
self._empty_event.wait(timeout=timeout)
greenlets = list(self.greenlets) if raise_error else ()
result = self._empty_event.wait(timeout=timeout)
for greenlet in greenlets:
if greenlet.exception is not None:
if hasattr(greenlet, '_raise_exception'):
greenlet._raise_exception()
raise greenlet.exception
return result
def kill(self, exception=GreenletExit, block=True, timeout=None):
"""
......
......@@ -465,7 +465,8 @@ class TestJoinEmpty(greentest.TestCase):
def test(self):
p = pool.Pool()
p.join()
res = p.join()
self.assertTrue(res, "empty should return true")
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)
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():
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