1 from .log import logger
4 'CancelledError', 'TimeoutError',
5 'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
8 # Argument for default thread pool executor creation.
12 import concurrent.futures
13 import concurrent.futures._base
15 FIRST_COMPLETED = 'FIRST_COMPLETED'
16 FIRST_EXCEPTION = 'FIRST_EXCEPTION'
17 ALL_COMPLETED = 'ALL_COMPLETED'
20 def __init__(self, callback, args):
22 self._result = callback(*args)
23 self._exception = None
24 except Exception as err:
36 return self._exception
39 if self._exception is not None:
44 def add_done_callback(self, callback):
47 class Error(Exception):
48 """Base class for all future-related exceptions."""
51 class CancelledError(Error):
52 """The Future was cancelled."""
55 class TimeoutError(Error):
56 """The operation exceeded the given deadline."""
59 class SynchronousExecutor:
61 Synchronous executor: submit() blocks until it gets the result.
63 def submit(self, callback, *args):
64 return Future(callback, args)
66 def shutdown(self, wait):
69 def get_default_executor():
70 logger.error("concurrent.futures module is missing: "
71 "use a synchrounous executor as fallback!")
72 return SynchronousExecutor()
74 FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED
75 FIRST_EXCEPTION = concurrent.futures.FIRST_EXCEPTION
76 ALL_COMPLETED = concurrent.futures.ALL_COMPLETED
78 Future = concurrent.futures.Future
79 Error = concurrent.futures._base.Error
80 CancelledError = concurrent.futures.CancelledError
81 TimeoutError = concurrent.futures.TimeoutError
83 def get_default_executor():
84 return concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS)