3 from __future__ import annotations
5 import concurrent.futures as fut
8 from typing import Callable, List, TypeVar
10 from overrides import overrides
12 # This module is commonly used by others in here and should avoid
13 # taking any unnecessary dependencies back on them.
14 from deferred_operand import DeferredOperand
17 logger = logging.getLogger(__name__)
23 futures: List[SmartFuture],
25 callback: Callable = None,
26 log_exceptions: bool = True,
29 smart_future_by_real_future = {}
30 completed_futures = set()
32 real_futures.append(_.wrapped_future)
33 smart_future_by_real_future[_.wrapped_future] = _
34 while len(completed_futures) != len(real_futures):
35 newly_completed_futures = concurrent.futures.as_completed(real_futures)
36 for f in newly_completed_futures:
37 if callback is not None:
39 completed_futures.add(f)
40 if log_exceptions and not f.cancelled():
41 exception = f.exception()
42 if exception is not None:
43 logger.exception(exception)
44 traceback.print_tb(exception.__traceback__)
45 yield smart_future_by_real_future[f]
46 if callback is not None:
52 futures: List[SmartFuture],
54 log_exceptions: bool = True,
56 real_futures = [x.wrapped_future for x in futures]
57 (done, not_done) = concurrent.futures.wait(
58 real_futures, timeout=None, return_when=concurrent.futures.ALL_COMPLETED
61 for f in real_futures:
63 exception = f.exception()
64 if exception is not None:
65 logger.exception(exception)
66 traceback.print_tb(exception.__traceback__)
67 assert len(done) == len(real_futures)
68 assert len(not_done) == 0
71 class SmartFuture(DeferredOperand):
72 """This is a SmartFuture, a class that wraps a normal Future and can
73 then be used, mostly, like a normal (non-Future) identifier.
75 Using a FutureWrapper in expressions will block and wait until
76 the result of the deferred operation is known.
79 def __init__(self, wrapped_future: fut.Future) -> None:
80 self.wrapped_future = wrapped_future
81 self.id = id_generator.get("smart_future_id")
83 def get_id(self) -> int:
86 def is_ready(self) -> bool:
87 return self.wrapped_future.done()
89 # You shouldn't have to call this; instead, have a look at defining a
90 # method on DeferredOperand base class.
92 def _resolve(self, *, timeout=None) -> T:
93 return self.wrapped_future.result(timeout)