3 from __future__ import annotations
4 from collections.abc import Mapping
5 import concurrent.futures as fut
7 from typing import Callable, List, TypeVar
9 from deferred_operand import DeferredOperand
15 def wait_many(futures: List[SmartFuture], *, callback: Callable = None):
16 finished: Mapping[int, bool] = {}
20 if not finished.get(future.get_id(), False):
22 finished[future.get_id()] = True
25 if callback is not None:
31 if len(finished) == len(futures):
32 if callback is not None:
37 class SmartFuture(DeferredOperand):
38 """This is a SmartFuture, a class that wraps a normal Future and can
39 then be used, mostly, like a normal (non-Future) identifier.
41 Using a FutureWrapper in expressions will block and wait until
42 the result of the deferred operation is known.
45 def __init__(self, wrapped_future: fut.Future) -> None:
46 self.wrapped_future = wrapped_future
47 self.id = id_generator.get("smart_future_id")
49 def get_id(self) -> int:
52 def is_ready(self) -> bool:
53 return self.wrapped_future.done()
55 # You shouldn't have to call this; instead, have a look at defining a
56 # method on DeferredOperand base class.
57 def _resolve(self, *, timeout=None) -> T:
58 return self.wrapped_future.result(timeout)