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):