From 5e1fe405a21997d7f3c75702dcacc1bcf80d5bac Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sat, 23 Aug 2014 18:36:39 -0700 Subject: [PATCH] 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 --- taskflow/utils/async_utils.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/taskflow/utils/async_utils.py b/taskflow/utils/async_utils.py index 0599870d..6d280dfe 100644 --- a/taskflow/utils/async_utils.py +++ b/taskflow/utils/async_utils.py @@ -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):