efficient vim config
[dotfiles/.git] / .local / lib / python2.7 / site-packages / trollius / executor.py
1 from .log import logger
2
3 __all__ = (
4     'CancelledError', 'TimeoutError',
5     'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
6     )
7
8 # Argument for default thread pool executor creation.
9 _MAX_WORKERS = 5
10
11 try:
12     import concurrent.futures
13     import concurrent.futures._base
14 except ImportError:
15     FIRST_COMPLETED = 'FIRST_COMPLETED'
16     FIRST_EXCEPTION = 'FIRST_EXCEPTION'
17     ALL_COMPLETED = 'ALL_COMPLETED'
18
19     class Future(object):
20         def __init__(self, callback, args):
21             try:
22                 self._result = callback(*args)
23                 self._exception = None
24             except Exception as err:
25                 self._result = None
26                 self._exception = err
27             self.callbacks = []
28
29         def cancelled(self):
30             return False
31
32         def done(self):
33             return True
34
35         def exception(self):
36             return self._exception
37
38         def result(self):
39             if self._exception is not None:
40                 raise self._exception
41             else:
42                 return self._result
43
44         def add_done_callback(self, callback):
45             callback(self)
46
47     class Error(Exception):
48         """Base class for all future-related exceptions."""
49         pass
50
51     class CancelledError(Error):
52         """The Future was cancelled."""
53         pass
54
55     class TimeoutError(Error):
56         """The operation exceeded the given deadline."""
57         pass
58
59     class SynchronousExecutor:
60         """
61         Synchronous executor: submit() blocks until it gets the result.
62         """
63         def submit(self, callback, *args):
64             return Future(callback, args)
65
66         def shutdown(self, wait):
67             pass
68
69     def get_default_executor():
70         logger.error("concurrent.futures module is missing: "
71                      "use a synchrounous executor as fallback!")
72         return SynchronousExecutor()
73 else:
74     FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED
75     FIRST_EXCEPTION = concurrent.futures.FIRST_EXCEPTION
76     ALL_COMPLETED = concurrent.futures.ALL_COMPLETED
77
78     Future = concurrent.futures.Future
79     Error = concurrent.futures._base.Error
80     CancelledError = concurrent.futures.CancelledError
81     TimeoutError = concurrent.futures.TimeoutError
82
83     def get_default_executor():
84         return concurrent.futures.ThreadPoolExecutor(_MAX_WORKERS)