Raise a runtime error when mixed green/non-green futures

To avoid the dead-lock scenario when green futures are being
waited on in the same call as non-green futures just disallow
this rare and unlikely scenario in the first place.

Change-Id: I5b57f7aae94ae40d22047b0b085e4d99e034c2a6
This commit is contained in:
Joshua Harlow
2014-08-23 18:36:39 -07:00
committed by Joshua Harlow
parent 36124cf2b4
commit 5e1fe405a2

View File

@@ -22,16 +22,25 @@ from taskflow.utils import eventlet_utils as eu
def wait_for_any(fs, timeout=None):
"""Wait for one of the futures to complete.
Works correctly with both green and non-green futures.
Works correctly with both green and non-green futures (but not both
together, since this can't be guaranteed to avoid dead-lock due to how
the waiting implementations are different when green threads are being
used).
Returns pair (done, not_done).
Returns pair (done futures, not done futures).
"""
any_green = any(isinstance(f, eu.GreenFuture) for f in fs)
if any_green:
return eu.wait_for_any(fs, timeout=timeout)
else:
green_fs = sum(1 for f in fs if isinstance(f, eu.GreenFuture))
if not green_fs:
return tuple(futures.wait(fs, timeout=timeout,
return_when=futures.FIRST_COMPLETED))
else:
non_green_fs = len(fs) - green_fs
if non_green_fs:
raise RuntimeError("Can not wait on %s green futures and %s"
" non-green futures in the same `wait_for_any`"
" call" % (green_fs, non_green_fs))
else:
return eu.wait_for_any(fs, timeout=timeout)
def make_completed_future(result):