|
14 | 14 |
|
15 | 15 | from __future__ import absolute_import
|
16 | 16 |
|
17 |
| -import threading |
18 |
| -import uuid |
| 17 | +import concurrent.futures |
19 | 18 |
|
20 | 19 | import google.api_core.future
|
21 |
| -from google.cloud.pubsub_v1.publisher import exceptions |
22 | 20 |
|
23 | 21 |
|
24 |
| -class Future(google.api_core.future.Future): |
| 22 | +class Future(concurrent.futures.Future, google.api_core.future.Future): |
25 | 23 | """Encapsulation of the asynchronous execution of an action.
|
26 | 24 |
|
27 | 25 | This object is returned from asychronous Pub/Sub calls, and is the
|
28 | 26 | interface to determine the status of those calls.
|
29 | 27 |
|
30 | 28 | This object should not be created directly, but is returned by other
|
31 | 29 | methods in this library.
|
32 |
| -
|
33 |
| - Args: |
34 |
| - completed (Optional[Any]): An event, with the same interface as |
35 |
| - :class:`threading.Event`. This is provided so that callers |
36 |
| - with different concurrency models (e.g. ``threading`` or |
37 |
| - ``multiprocessing``) can supply an event that is compatible |
38 |
| - with that model. The ``wait()`` and ``set()`` methods will be |
39 |
| - used. If this argument is not provided, then a new |
40 |
| - :class:`threading.Event` will be created and used. |
41 | 30 | """
|
42 | 31 |
|
43 |
| - # This could be a sentinel object or None, but the sentinel object's ID |
44 |
| - # can change if the process is forked, and None has the possibility of |
45 |
| - # actually being a result. |
46 |
| - _SENTINEL = uuid.uuid4() |
47 |
| - |
48 |
| - def __init__(self, completed=None): |
49 |
| - self._result = self._SENTINEL |
50 |
| - self._exception = self._SENTINEL |
51 |
| - self._callbacks = [] |
52 |
| - if completed is None: |
53 |
| - completed = threading.Event() |
54 |
| - self._completed = completed |
55 |
| - |
56 |
| - def cancel(self): |
57 |
| - """Actions in Pub/Sub generally may not be canceled. |
58 |
| -
|
59 |
| - This method always returns False. |
60 |
| - """ |
61 |
| - return False |
62 |
| - |
63 |
| - def cancelled(self): |
64 |
| - """Actions in Pub/Sub generally may not be canceled. |
65 |
| -
|
66 |
| - This method always returns False. |
67 |
| - """ |
68 |
| - return False |
69 |
| - |
70 | 32 | def running(self):
|
71 |
| - """Actions in Pub/Sub generally may not be canceled. |
| 33 | + """Return ``True`` if the associated Pub/Sub action has not yet completed. |
72 | 34 |
|
73 |
| - Returns: |
74 |
| - bool: ``True`` if this method has not yet completed, or |
75 |
| - ``False`` if it has completed. |
| 35 | + Returns: bool: |
76 | 36 | """
|
77 | 37 | return not self.done()
|
78 | 38 |
|
79 |
| - def done(self): |
80 |
| - """Return True the future is done, False otherwise. |
81 |
| -
|
82 |
| - This still returns True in failure cases; checking :meth:`result` or |
83 |
| - :meth:`exception` is the canonical way to assess success or failure. |
84 |
| - """ |
85 |
| - return self._exception != self._SENTINEL or self._result != self._SENTINEL |
86 |
| - |
87 |
| - def result(self, timeout=None): |
88 |
| - """Resolve the future and return a value where appropriate. |
89 |
| -
|
90 |
| - Args: |
91 |
| - timeout (Union[int, float]): The number of seconds before this call |
92 |
| - times out and raises TimeoutError. |
93 |
| -
|
94 |
| - Raises: |
95 |
| - concurrent.futures.TimeoutError: If the request times out. |
96 |
| - Exception: For undefined exceptions in the underlying |
97 |
| - call execution. |
98 |
| - """ |
99 |
| - # Attempt to get the exception if there is one. |
100 |
| - # If there is not one, then we know everything worked, and we can |
101 |
| - # return an appropriate value. |
102 |
| - err = self.exception(timeout=timeout) |
103 |
| - if err is None: |
104 |
| - return self._result |
105 |
| - raise err |
106 |
| - |
107 |
| - def exception(self, timeout=None): |
108 |
| - """Return the exception raised by the call, if any. |
109 |
| -
|
110 |
| - Args: |
111 |
| - timeout (Union[int, float]): The number of seconds before this call |
112 |
| - times out and raises TimeoutError. |
113 |
| -
|
114 |
| - Raises: |
115 |
| - concurrent.futures.TimeoutError: If the request times out. |
116 |
| -
|
117 |
| - Returns: |
118 |
| - Exception: The exception raised by the call, if any. |
119 |
| - """ |
120 |
| - # Wait until the future is done. |
121 |
| - if not self._completed.wait(timeout=timeout): |
122 |
| - raise exceptions.TimeoutError("Timed out waiting for result.") |
123 |
| - |
124 |
| - # If the batch completed successfully, this should return None. |
125 |
| - if self._result != self._SENTINEL: |
126 |
| - return None |
127 |
| - |
128 |
| - # Okay, this batch had an error; this should return it. |
129 |
| - return self._exception |
130 |
| - |
131 |
| - def add_done_callback(self, callback): |
132 |
| - """Attach the provided callable to the future. |
133 |
| -
|
134 |
| - The provided function is called, with this future as its only argument, |
135 |
| - when the future finishes running. |
136 |
| -
|
137 |
| - Args: |
138 |
| - callback (Callable): The function to call. |
139 |
| -
|
140 |
| - Returns: |
141 |
| - None |
142 |
| - """ |
143 |
| - if self.done(): |
144 |
| - return callback(self) |
145 |
| - self._callbacks.append(callback) |
| 39 | + def set_running_or_notify_cancel(self): |
| 40 | + raise NotImplementedError( |
| 41 | + "Only used by executors from `concurrent.futures` package." |
| 42 | + ) |
146 | 43 |
|
147 | 44 | def set_result(self, result):
|
148 |
| - """Set the result of the future to the provided result. |
| 45 | + """Set the return value of work associated with the future. |
149 | 46 |
|
150 |
| - Args: |
151 |
| - result (Any): The result |
| 47 | + Do not use this method, it should only be used internally by the library and its |
| 48 | + unit tests. |
152 | 49 | """
|
153 |
| - # Sanity check: A future can only complete once. |
154 |
| - if self.done(): |
155 |
| - raise RuntimeError("set_result can only be called once.") |
156 |
| - |
157 |
| - # Set the result and trigger the future. |
158 |
| - self._result = result |
159 |
| - self._trigger() |
| 50 | + return super().set_result(result=result) |
160 | 51 |
|
161 | 52 | def set_exception(self, exception):
|
162 |
| - """Set the result of the future to the given exception. |
163 |
| -
|
164 |
| - Args: |
165 |
| - exception (:exc:`Exception`): The exception raised. |
166 |
| - """ |
167 |
| - # Sanity check: A future can only complete once. |
168 |
| - if self.done(): |
169 |
| - raise RuntimeError("set_exception can only be called once.") |
170 |
| - |
171 |
| - # Set the exception and trigger the future. |
172 |
| - self._exception = exception |
173 |
| - self._trigger() |
174 |
| - |
175 |
| - def _trigger(self): |
176 |
| - """Trigger all callbacks registered to this Future. |
177 |
| -
|
178 |
| - This method is called internally by the batch once the batch |
179 |
| - completes. |
| 53 | + """Set the result of the future as being the given exception. |
180 | 54 |
|
181 |
| - Args: |
182 |
| - message_id (str): The message ID, as a string. |
| 55 | + Do not use this method, it should only be used internally by the library and its |
| 56 | + unit tests. |
183 | 57 | """
|
184 |
| - self._completed.set() |
185 |
| - for callback in self._callbacks: |
186 |
| - callback(self) |
| 58 | + return super().set_exception(exception=exception) |
0 commit comments