summaryrefslogtreecommitdiff
path: root/smart_future.py
diff options
context:
space:
mode:
authorScott <[email protected]>2022-01-30 14:49:52 -0800
committerScott <[email protected]>2022-01-30 14:49:52 -0800
commit822454f580c1ff9eb207b8da46cdfae24e30cde1 (patch)
treee446d8f51230e590e8a9ebed240d5fd795ef0d75 /smart_future.py
parentc8726266418d0e7f287cbe7f9abb017eedb68921 (diff)
Optionally surface exceptions that happen under executors by reading
them off the returned futures.
Diffstat (limited to 'smart_future.py')
-rw-r--r--smart_future.py29
1 files changed, 27 insertions, 2 deletions
diff --git a/smart_future.py b/smart_future.py
index 8f23e77..c96c5a7 100644
--- a/smart_future.py
+++ b/smart_future.py
@@ -3,6 +3,8 @@
from __future__ import annotations
import concurrent
import concurrent.futures as fut
+import logging
+import traceback
from typing import Callable, List, TypeVar
from overrides import overrides
@@ -12,10 +14,17 @@ from overrides import overrides
from deferred_operand import DeferredOperand
import id_generator
+logger = logging.getLogger(__name__)
+
T = TypeVar('T')
-def wait_any(futures: List[SmartFuture], *, callback: Callable = None):
+def wait_any(
+ futures: List[SmartFuture],
+ *,
+ callback: Callable = None,
+ log_exceptions: bool = True,
+):
real_futures = []
smart_future_by_real_future = {}
completed_futures = set()
@@ -28,17 +37,33 @@ def wait_any(futures: List[SmartFuture], *, callback: Callable = None):
if callback is not None:
callback()
completed_futures.add(f)
+ if log_exceptions and not f.cancelled():
+ exception = f.exception()
+ if exception is not None:
+ logger.exception(exception)
+ traceback.print_tb(exception.__traceback__)
yield smart_future_by_real_future[f]
if callback is not None:
callback()
return
-def wait_all(futures: List[SmartFuture]) -> None:
+def wait_all(
+ futures: List[SmartFuture],
+ *,
+ log_exceptions: bool = True,
+) -> None:
real_futures = [x.wrapped_future for x in futures]
(done, not_done) = concurrent.futures.wait(
real_futures, timeout=None, return_when=concurrent.futures.ALL_COMPLETED
)
+ if log_exceptions:
+ for f in real_futures:
+ if not f.cancelled():
+ exception = f.exception()
+ if exception is not None:
+ logger.exception(exception)
+ traceback.print_tb(exception.__traceback__)
assert len(done) == len(real_futures)
assert len(not_done) == 0