X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=smart_future.py;h=f11be17bc0fabfeb4a111e4b3356bbcb1ed1633a;hb=eb9e6df32ed696158bf34dba6464277b648f5c74;hp=f1ffee1c63250b4fb0d0be319a8090ce406f5fc0;hpb=497fb9e21f45ec08e1486abaee6dfa7b20b8a691;p=python_utils.git diff --git a/smart_future.py b/smart_future.py index f1ffee1..f11be17 100644 --- a/smart_future.py +++ b/smart_future.py @@ -6,13 +6,17 @@ import concurrent.futures as fut import time from typing import Callable, List, TypeVar +from overrides import overrides + +# This module is commonly used by others in here and should avoid +# taking any unnecessary dependencies back on them. from deferred_operand import DeferredOperand import id_generator T = TypeVar('T') -def wait_many(futures: List[SmartFuture], *, callback: Callable = None): +def wait_any(futures: List[SmartFuture], *, callback: Callable = None): finished: Mapping[int, bool] = {} x = 0 while True: @@ -34,6 +38,16 @@ def wait_many(futures: List[SmartFuture], *, callback: Callable = None): return +def wait_all(futures: List[SmartFuture]) -> None: + done_set = set() + while len(done_set) < len(futures): + for future in futures: + i = future.get_id() + if i not in done_set and future.wrapped_future.done(): + done_set.add(i) + time.sleep(0.1) + + class SmartFuture(DeferredOperand): """This is a SmartFuture, a class that wraps a normal Future and can then be used, mostly, like a normal (non-Future) identifier. @@ -54,5 +68,6 @@ class SmartFuture(DeferredOperand): # You shouldn't have to call this; instead, have a look at defining a # method on DeferredOperand base class. + @overrides def _resolve(self, *, timeout=None) -> T: return self.wrapped_future.result(timeout)