Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 1 | """A Future class similar to the one in PEP 3148.""" |
| 2 | |
| 3 | __all__ = ['CancelledError', 'TimeoutError', |
| 4 | 'InvalidStateError', |
| 5 | 'Future', 'wrap_future', |
| 6 | ] |
| 7 | |
| 8 | import concurrent.futures._base |
| 9 | import logging |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 10 | import sys |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 11 | import traceback |
| 12 | |
| 13 | from . import events |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 14 | from .log import logger |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 15 | |
| 16 | # States for Future. |
| 17 | _PENDING = 'PENDING' |
| 18 | _CANCELLED = 'CANCELLED' |
| 19 | _FINISHED = 'FINISHED' |
| 20 | |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 21 | _PY34 = sys.version_info >= (3, 4) |
| 22 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 23 | # TODO: Do we really want to depend on concurrent.futures internals? |
| 24 | Error = concurrent.futures._base.Error |
| 25 | CancelledError = concurrent.futures.CancelledError |
| 26 | TimeoutError = concurrent.futures.TimeoutError |
| 27 | |
| 28 | STACK_DEBUG = logging.DEBUG - 1 # heavy-duty debugging |
| 29 | |
| 30 | |
| 31 | class InvalidStateError(Error): |
| 32 | """The operation is not allowed in this state.""" |
| 33 | # TODO: Show the future, its state, the method, and the required state. |
| 34 | |
| 35 | |
| 36 | class _TracebackLogger: |
| 37 | """Helper to log a traceback upon destruction if not cleared. |
| 38 | |
| 39 | This solves a nasty problem with Futures and Tasks that have an |
| 40 | exception set: if nobody asks for the exception, the exception is |
| 41 | never logged. This violates the Zen of Python: 'Errors should |
| 42 | never pass silently. Unless explicitly silenced.' |
| 43 | |
| 44 | However, we don't want to log the exception as soon as |
| 45 | set_exception() is called: if the calling code is written |
| 46 | properly, it will get the exception and handle it properly. But |
| 47 | we *do* want to log it if result() or exception() was never called |
| 48 | -- otherwise developers waste a lot of time wondering why their |
| 49 | buggy code fails silently. |
| 50 | |
| 51 | An earlier attempt added a __del__() method to the Future class |
| 52 | itself, but this backfired because the presence of __del__() |
| 53 | prevents garbage collection from breaking cycles. A way out of |
| 54 | this catch-22 is to avoid having a __del__() method on the Future |
| 55 | class itself, but instead to have a reference to a helper object |
| 56 | with a __del__() method that logs the traceback, where we ensure |
| 57 | that the helper object doesn't participate in cycles, and only the |
| 58 | Future has a reference to it. |
| 59 | |
| 60 | The helper object is added when set_exception() is called. When |
| 61 | the Future is collected, and the helper is present, the helper |
| 62 | object is also collected, and its __del__() method will log the |
| 63 | traceback. When the Future's result() or exception() method is |
| 64 | called (and a helper object is present), it removes the the helper |
| 65 | object, after calling its clear() method to prevent it from |
| 66 | logging. |
| 67 | |
| 68 | One downside is that we do a fair amount of work to extract the |
| 69 | traceback from the exception, even when it is never logged. It |
| 70 | would seem cheaper to just store the exception object, but that |
| 71 | references the traceback, which references stack frames, which may |
| 72 | reference the Future, which references the _TracebackLogger, and |
| 73 | then the _TracebackLogger would be included in a cycle, which is |
| 74 | what we're trying to avoid! As an optimization, we don't |
| 75 | immediately format the exception; we only do the work when |
| 76 | activate() is called, which call is delayed until after all the |
| 77 | Future's callbacks have run. Since usually a Future has at least |
| 78 | one callback (typically set by 'yield from') and usually that |
| 79 | callback extracts the callback, thereby removing the need to |
| 80 | format the exception. |
| 81 | |
| 82 | PS. I don't claim credit for this solution. I first heard of it |
| 83 | in a discussion about closing files when they are collected. |
| 84 | """ |
| 85 | |
| 86 | __slots__ = ['exc', 'tb'] |
| 87 | |
| 88 | def __init__(self, exc): |
| 89 | self.exc = exc |
| 90 | self.tb = None |
| 91 | |
| 92 | def activate(self): |
| 93 | exc = self.exc |
| 94 | if exc is not None: |
| 95 | self.exc = None |
| 96 | self.tb = traceback.format_exception(exc.__class__, exc, |
| 97 | exc.__traceback__) |
| 98 | |
| 99 | def clear(self): |
| 100 | self.exc = None |
| 101 | self.tb = None |
| 102 | |
| 103 | def __del__(self): |
| 104 | if self.tb: |
Guido van Rossum | fc29e0f | 2013-10-17 15:39:45 -0700 | [diff] [blame] | 105 | logger.error('Future/Task exception was never retrieved:\n%s', |
| 106 | ''.join(self.tb)) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | class Future: |
| 110 | """This class is *almost* compatible with concurrent.futures.Future. |
| 111 | |
| 112 | Differences: |
| 113 | |
| 114 | - result() and exception() do not take a timeout argument and |
| 115 | raise an exception when the future isn't done yet. |
| 116 | |
| 117 | - Callbacks registered with add_done_callback() are always called |
| 118 | via the event loop's call_soon_threadsafe(). |
| 119 | |
| 120 | - This class is not compatible with the wait() and as_completed() |
| 121 | methods in the concurrent.futures package. |
| 122 | |
| 123 | (In Python 3.4 or later we may be able to unify the implementations.) |
| 124 | """ |
| 125 | |
| 126 | # Class variables serving as defaults for instance variables. |
| 127 | _state = _PENDING |
| 128 | _result = None |
| 129 | _exception = None |
| 130 | _loop = None |
| 131 | |
| 132 | _blocking = False # proper use of future (yield vs yield from) |
| 133 | |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 134 | _traceback = None # Used for Python 3.4 and later |
| 135 | _tb_logger = None # Used for Python 3.3 only |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 136 | |
| 137 | def __init__(self, *, loop=None): |
| 138 | """Initialize the future. |
| 139 | |
| 140 | The optional event_loop argument allows to explicitly set the event |
| 141 | loop object used by the future. If it's not provided, the future uses |
| 142 | the default event loop. |
| 143 | """ |
| 144 | if loop is None: |
| 145 | self._loop = events.get_event_loop() |
| 146 | else: |
| 147 | self._loop = loop |
| 148 | self._callbacks = [] |
| 149 | |
| 150 | def __repr__(self): |
| 151 | res = self.__class__.__name__ |
| 152 | if self._state == _FINISHED: |
| 153 | if self._exception is not None: |
| 154 | res += '<exception={!r}>'.format(self._exception) |
| 155 | else: |
| 156 | res += '<result={!r}>'.format(self._result) |
| 157 | elif self._callbacks: |
| 158 | size = len(self._callbacks) |
| 159 | if size > 2: |
| 160 | res += '<{}, [{}, <{} more>, {}]>'.format( |
| 161 | self._state, self._callbacks[0], |
| 162 | size-2, self._callbacks[-1]) |
| 163 | else: |
| 164 | res += '<{}, {}>'.format(self._state, self._callbacks) |
| 165 | else: |
| 166 | res += '<{}>'.format(self._state) |
| 167 | return res |
| 168 | |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 169 | if _PY34: |
| 170 | def __del__(self): |
| 171 | if self._traceback is not None: |
| 172 | logger.error('Future/Task exception was never retrieved:\n%s', |
| 173 | ''.join(self._traceback)) |
| 174 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 175 | def cancel(self): |
| 176 | """Cancel the future and schedule callbacks. |
| 177 | |
| 178 | If the future is already done or cancelled, return False. Otherwise, |
| 179 | change the future's state to cancelled, schedule the callbacks and |
| 180 | return True. |
| 181 | """ |
| 182 | if self._state != _PENDING: |
| 183 | return False |
| 184 | self._state = _CANCELLED |
| 185 | self._schedule_callbacks() |
| 186 | return True |
| 187 | |
| 188 | def _schedule_callbacks(self): |
| 189 | """Internal: Ask the event loop to call all callbacks. |
| 190 | |
| 191 | The callbacks are scheduled to be called as soon as possible. Also |
| 192 | clears the callback list. |
| 193 | """ |
| 194 | callbacks = self._callbacks[:] |
| 195 | if not callbacks: |
| 196 | return |
| 197 | |
| 198 | self._callbacks[:] = [] |
| 199 | for callback in callbacks: |
| 200 | self._loop.call_soon(callback, self) |
| 201 | |
| 202 | def cancelled(self): |
| 203 | """Return True if the future was cancelled.""" |
| 204 | return self._state == _CANCELLED |
| 205 | |
| 206 | # Don't implement running(); see http://bugs.python.org/issue18699 |
| 207 | |
| 208 | def done(self): |
| 209 | """Return True if the future is done. |
| 210 | |
| 211 | Done means either that a result / exception are available, or that the |
| 212 | future was cancelled. |
| 213 | """ |
| 214 | return self._state != _PENDING |
| 215 | |
| 216 | def result(self): |
| 217 | """Return the result this future represents. |
| 218 | |
| 219 | If the future has been cancelled, raises CancelledError. If the |
| 220 | future's result isn't yet available, raises InvalidStateError. If |
| 221 | the future is done and has an exception set, this exception is raised. |
| 222 | """ |
| 223 | if self._state == _CANCELLED: |
| 224 | raise CancelledError |
| 225 | if self._state != _FINISHED: |
| 226 | raise InvalidStateError('Result is not ready.') |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 227 | self._traceback = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 228 | if self._tb_logger is not None: |
| 229 | self._tb_logger.clear() |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 230 | self._tb_logger = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 231 | if self._exception is not None: |
| 232 | raise self._exception |
| 233 | return self._result |
| 234 | |
| 235 | def exception(self): |
| 236 | """Return the exception that was set on this future. |
| 237 | |
| 238 | The exception (or None if no exception was set) is returned only if |
| 239 | the future is done. If the future has been cancelled, raises |
| 240 | CancelledError. If the future isn't done yet, raises |
| 241 | InvalidStateError. |
| 242 | """ |
| 243 | if self._state == _CANCELLED: |
| 244 | raise CancelledError |
| 245 | if self._state != _FINISHED: |
| 246 | raise InvalidStateError('Exception is not set.') |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 247 | self._traceback = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 248 | if self._tb_logger is not None: |
| 249 | self._tb_logger.clear() |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 250 | self._tb_logger = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 251 | return self._exception |
| 252 | |
| 253 | def add_done_callback(self, fn): |
| 254 | """Add a callback to be run when the future becomes done. |
| 255 | |
| 256 | The callback is called with a single argument - the future object. If |
| 257 | the future is already done when this is called, the callback is |
| 258 | scheduled with call_soon. |
| 259 | """ |
| 260 | if self._state != _PENDING: |
| 261 | self._loop.call_soon(fn, self) |
| 262 | else: |
| 263 | self._callbacks.append(fn) |
| 264 | |
| 265 | # New method not in PEP 3148. |
| 266 | |
| 267 | def remove_done_callback(self, fn): |
| 268 | """Remove all instances of a callback from the "call when done" list. |
| 269 | |
| 270 | Returns the number of callbacks removed. |
| 271 | """ |
| 272 | filtered_callbacks = [f for f in self._callbacks if f != fn] |
| 273 | removed_count = len(self._callbacks) - len(filtered_callbacks) |
| 274 | if removed_count: |
| 275 | self._callbacks[:] = filtered_callbacks |
| 276 | return removed_count |
| 277 | |
| 278 | # So-called internal methods (note: no set_running_or_notify_cancel()). |
| 279 | |
| 280 | def set_result(self, result): |
| 281 | """Mark the future done and set its result. |
| 282 | |
| 283 | If the future is already done when this method is called, raises |
| 284 | InvalidStateError. |
| 285 | """ |
| 286 | if self._state != _PENDING: |
| 287 | raise InvalidStateError('{}: {!r}'.format(self._state, self)) |
| 288 | self._result = result |
| 289 | self._state = _FINISHED |
| 290 | self._schedule_callbacks() |
| 291 | |
| 292 | def set_exception(self, exception): |
| 293 | """Mark the future done and set an exception. |
| 294 | |
| 295 | If the future is already done when this method is called, raises |
| 296 | InvalidStateError. |
| 297 | """ |
| 298 | if self._state != _PENDING: |
| 299 | raise InvalidStateError('{}: {!r}'.format(self._state, self)) |
| 300 | self._exception = exception |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 301 | self._state = _FINISHED |
| 302 | self._schedule_callbacks() |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 303 | if _PY34: |
| 304 | self._traceback = traceback.format_exception( |
Guido van Rossum | 9ba75db | 2013-12-19 13:49:32 -0800 | [diff] [blame] | 305 | exception.__class__, exception, exception.__traceback__) |
Victor Stinner | 4c3c699 | 2013-12-19 22:42:40 +0100 | [diff] [blame] | 306 | else: |
| 307 | self._tb_logger = _TracebackLogger(exception) |
| 308 | # Arrange for the logger to be activated after all callbacks |
| 309 | # have had a chance to call result() or exception(). |
| 310 | self._loop.call_soon(self._tb_logger.activate) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 311 | |
| 312 | # Truly internal methods. |
| 313 | |
| 314 | def _copy_state(self, other): |
| 315 | """Internal helper to copy state from another Future. |
| 316 | |
| 317 | The other Future may be a concurrent.futures.Future. |
| 318 | """ |
| 319 | assert other.done() |
Guido van Rossum | 7a46564 | 2013-11-22 11:47:22 -0800 | [diff] [blame] | 320 | if self.cancelled(): |
| 321 | return |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 322 | assert not self.done() |
| 323 | if other.cancelled(): |
| 324 | self.cancel() |
| 325 | else: |
| 326 | exception = other.exception() |
| 327 | if exception is not None: |
| 328 | self.set_exception(exception) |
| 329 | else: |
| 330 | result = other.result() |
| 331 | self.set_result(result) |
| 332 | |
| 333 | def __iter__(self): |
| 334 | if not self.done(): |
| 335 | self._blocking = True |
| 336 | yield self # This tells Task to wait for completion. |
| 337 | assert self.done(), "yield from wasn't used with future" |
| 338 | return self.result() # May raise too. |
| 339 | |
| 340 | |
| 341 | def wrap_future(fut, *, loop=None): |
| 342 | """Wrap concurrent.futures.Future object.""" |
| 343 | if isinstance(fut, Future): |
| 344 | return fut |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 345 | assert isinstance(fut, concurrent.futures.Future), \ |
| 346 | 'concurrent.futures.Future is expected, got {!r}'.format(fut) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 347 | if loop is None: |
| 348 | loop = events.get_event_loop() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 349 | new_future = Future(loop=loop) |
Guido van Rossum | 7a46564 | 2013-11-22 11:47:22 -0800 | [diff] [blame] | 350 | |
| 351 | def _check_cancel_other(f): |
| 352 | if f.cancelled(): |
| 353 | fut.cancel() |
| 354 | |
| 355 | new_future.add_done_callback(_check_cancel_other) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 356 | fut.add_done_callback( |
| 357 | lambda future: loop.call_soon_threadsafe( |
| 358 | new_future._copy_state, fut)) |
| 359 | return new_future |