Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
concurrent.futures._base.Future Class Reference
Inheritance diagram for concurrent.futures._base.Future:
Inheritance graph
[legend]
Collaboration diagram for concurrent.futures._base.Future:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def cancel
def cancelled
def running
def done
def add_done_callback
def result
def exception
def set_running_or_notify_cancel
def set_result
def set_exception

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Private Member Functions

def _invoke_callbacks
def __get_result

Private Attributes

 _condition
 _state
 _result
 _exception
 _waiters
 _done_callbacks

Detailed Description

Represents the result of an asynchronous computation.

Definition at line 278 of file _base.py.


Constructor & Destructor Documentation

Initializes the future. Should not be called by clients.

Definition at line 281 of file _base.py.

00281 
00282     def __init__(self):
00283         """Initializes the future. Should not be called by clients."""
00284         self._condition = threading.Condition()
00285         self._state = PENDING
00286         self._result = None
00287         self._exception = None
00288         self._waiters = []
00289         self._done_callbacks = []

Here is the caller graph for this function:


Member Function Documentation

Definition at line 348 of file _base.py.

00348 
00349     def __get_result(self):
00350         if self._exception:
00351             raise self._exception
00352         else:
00353             return self._result

Here is the caller graph for this function:

Definition at line 297 of file _base.py.

00297 
00298     def __repr__(self):
00299         with self._condition:
00300             if self._state == FINISHED:
00301                 if self._exception:
00302                     return '<Future at %s state=%s raised %s>' % (
00303                         hex(id(self)),
00304                         _STATE_TO_DESCRIPTION_MAP[self._state],
00305                         self._exception.__class__.__name__)
00306                 else:
00307                     return '<Future at %s state=%s returned %s>' % (
00308                         hex(id(self)),
00309                         _STATE_TO_DESCRIPTION_MAP[self._state],
00310                         self._result.__class__.__name__)
00311             return '<Future at %s state=%s>' % (
00312                     hex(id(self)),
00313                    _STATE_TO_DESCRIPTION_MAP[self._state])

Definition at line 290 of file _base.py.

00290 
00291     def _invoke_callbacks(self):
00292         for callback in self._done_callbacks:
00293             try:
00294                 callback(self)
00295             except Exception:
00296                 LOGGER.exception('exception calling callback for %r', self)

Here is the caller graph for this function:

Attaches a callable that will be called when the future finishes.

Args:
    fn: A callable that will be called with this future as its only
argument when the future completes or is cancelled. The callable
will always be called by a thread in the same process in which
it was added. If the future has already completed or been
cancelled then the callable will be called immediately. These
callables are called in the order that they were added.

Definition at line 354 of file _base.py.

00354 
00355     def add_done_callback(self, fn):
00356         """Attaches a callable that will be called when the future finishes.
00357 
00358         Args:
00359             fn: A callable that will be called with this future as its only
00360                 argument when the future completes or is cancelled. The callable
00361                 will always be called by a thread in the same process in which
00362                 it was added. If the future has already completed or been
00363                 cancelled then the callable will be called immediately. These
00364                 callables are called in the order that they were added.
00365         """
00366         with self._condition:
00367             if self._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]:
00368                 self._done_callbacks.append(fn)
00369                 return
00370         fn(self)

Cancel the future if possible.

Returns True if the future was cancelled, False otherwise. A future
cannot be cancelled if it is running or has already completed.

Definition at line 314 of file _base.py.

00314 
00315     def cancel(self):
00316         """Cancel the future if possible.
00317 
00318         Returns True if the future was cancelled, False otherwise. A future
00319         cannot be cancelled if it is running or has already completed.
00320         """
00321         with self._condition:
00322             if self._state in [RUNNING, FINISHED]:
00323                 return False
00324 
00325             if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
00326                 return True
00327 
00328             self._state = CANCELLED
00329             self._condition.notify_all()
00330 
00331         self._invoke_callbacks()
00332         return True

Here is the call graph for this function:

Here is the caller graph for this function:

Return True if the future has cancelled.

Definition at line 333 of file _base.py.

00333 
00334     def cancelled(self):
00335         """Return True if the future has cancelled."""
00336         with self._condition:
00337             return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]

Return True of the future was cancelled or finished executing.

Definition at line 343 of file _base.py.

00343 
00344     def done(self):
00345         """Return True of the future was cancelled or finished executing."""
00346         with self._condition:
00347             return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]

Here is the caller graph for this function:

def concurrent.futures._base.Future.exception (   self,
  timeout = None 
)
Return the exception raised by the call that the future represents.

Args:
    timeout: The number of seconds to wait for the exception if the
future isn't done. If None, then there is no limit on the wait
time.

Returns:
    The exception raised by the call that the future represents or None
    if the call completed without raising.

Raises:
    CancelledError: If the future was cancelled.
    TimeoutError: If the future didn't finish executing before the given
timeout.

Definition at line 402 of file _base.py.

00402 
00403     def exception(self, timeout=None):
00404         """Return the exception raised by the call that the future represents.
00405 
00406         Args:
00407             timeout: The number of seconds to wait for the exception if the
00408                 future isn't done. If None, then there is no limit on the wait
00409                 time.
00410 
00411         Returns:
00412             The exception raised by the call that the future represents or None
00413             if the call completed without raising.
00414 
00415         Raises:
00416             CancelledError: If the future was cancelled.
00417             TimeoutError: If the future didn't finish executing before the given
00418                 timeout.
00419         """
00420 
00421         with self._condition:
00422             if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
00423                 raise CancelledError()
00424             elif self._state == FINISHED:
00425                 return self._exception
00426 
00427             self._condition.wait(timeout)
00428 
00429             if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
00430                 raise CancelledError()
00431             elif self._state == FINISHED:
00432                 return self._exception
00433             else:
00434                 raise TimeoutError()

def concurrent.futures._base.Future.result (   self,
  timeout = None 
)
Return the result of the call that the future represents.

Args:
    timeout: The number of seconds to wait for the result if the future
isn't done. If None, then there is no limit on the wait time.

Returns:
    The result of the call that the future represents.

Raises:
    CancelledError: If the future was cancelled.
    TimeoutError: If the future didn't finish executing before the given
timeout.
    Exception: If the call raised then that exception will be raised.

Definition at line 371 of file _base.py.

00371 
00372     def result(self, timeout=None):
00373         """Return the result of the call that the future represents.
00374 
00375         Args:
00376             timeout: The number of seconds to wait for the result if the future
00377                 isn't done. If None, then there is no limit on the wait time.
00378 
00379         Returns:
00380             The result of the call that the future represents.
00381 
00382         Raises:
00383             CancelledError: If the future was cancelled.
00384             TimeoutError: If the future didn't finish executing before the given
00385                 timeout.
00386             Exception: If the call raised then that exception will be raised.
00387         """
00388         with self._condition:
00389             if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
00390                 raise CancelledError()
00391             elif self._state == FINISHED:
00392                 return self.__get_result()
00393 
00394             self._condition.wait(timeout)
00395 
00396             if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
00397                 raise CancelledError()
00398             elif self._state == FINISHED:
00399                 return self.__get_result()
00400             else:
00401                 raise TimeoutError()

Here is the call graph for this function:

Return True if the future is currently executing.

Definition at line 338 of file _base.py.

00338 
00339     def running(self):
00340         """Return True if the future is currently executing."""
00341         with self._condition:
00342             return self._state == RUNNING

def concurrent.futures._base.Future.set_exception (   self,
  exception 
)
Sets the result of the future as being the given exception.

Should only be used by Executor implementations and unit tests.

Definition at line 489 of file _base.py.

00489 
00490     def set_exception(self, exception):
00491         """Sets the result of the future as being the given exception.
00492 
00493         Should only be used by Executor implementations and unit tests.
00494         """
00495         with self._condition:
00496             self._exception = exception
00497             self._state = FINISHED
00498             for waiter in self._waiters:
00499                 waiter.add_exception(self)
00500             self._condition.notify_all()
00501         self._invoke_callbacks()

Here is the call graph for this function:

def concurrent.futures._base.Future.set_result (   self,
  result 
)
Sets the return value of work associated with the future.

Should only be used by Executor implementations and unit tests.

Definition at line 476 of file _base.py.

00476 
00477     def set_result(self, result):
00478         """Sets the return value of work associated with the future.
00479 
00480         Should only be used by Executor implementations and unit tests.
00481         """
00482         with self._condition:
00483             self._result = result
00484             self._state = FINISHED
00485             for waiter in self._waiters:
00486                 waiter.add_result(self)
00487             self._condition.notify_all()
00488         self._invoke_callbacks()

Here is the call graph for this function:

Mark the future as running or process any cancel notifications.

Should only be used by Executor implementations and unit tests.

If the future has been cancelled (cancel() was called and returned
True) then any threads waiting on the future completing (though calls
to as_completed() or wait()) are notified and False is returned.

If the future was not cancelled then it is put in the running state
(future calls to running() will return True) and True is returned.

This method should be called by Executor implementations before
executing the work associated with this future. If this method returns
False then the work should not be executed.

Returns:
    False if the Future was cancelled, True otherwise.

Raises:
    RuntimeError: if this method was already called or if set_result()
or set_exception() was called.

Definition at line 436 of file _base.py.

00436 
00437     def set_running_or_notify_cancel(self):
00438         """Mark the future as running or process any cancel notifications.
00439 
00440         Should only be used by Executor implementations and unit tests.
00441 
00442         If the future has been cancelled (cancel() was called and returned
00443         True) then any threads waiting on the future completing (though calls
00444         to as_completed() or wait()) are notified and False is returned.
00445 
00446         If the future was not cancelled then it is put in the running state
00447         (future calls to running() will return True) and True is returned.
00448 
00449         This method should be called by Executor implementations before
00450         executing the work associated with this future. If this method returns
00451         False then the work should not be executed.
00452 
00453         Returns:
00454             False if the Future was cancelled, True otherwise.
00455 
00456         Raises:
00457             RuntimeError: if this method was already called or if set_result()
00458                 or set_exception() was called.
00459         """
00460         with self._condition:
00461             if self._state == CANCELLED:
00462                 self._state = CANCELLED_AND_NOTIFIED
00463                 for waiter in self._waiters:
00464                     waiter.add_cancelled(self)
00465                 # self._condition.notify_all() is not necessary because
00466                 # self.cancel() triggers a notification.
00467                 return False
00468             elif self._state == PENDING:
00469                 self._state = RUNNING
00470                 return True
00471             else:
00472                 LOGGER.critical('Future %s in unexpected state: %s',
00473                                 id(self.future),
00474                                 self.future._state)
00475                 raise RuntimeError('Future in unexpected state')


Member Data Documentation

Definition at line 283 of file _base.py.

Definition at line 288 of file _base.py.

Definition at line 286 of file _base.py.

Definition at line 285 of file _base.py.

Definition at line 284 of file _base.py.

Definition at line 287 of file _base.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.


The documentation for this class was generated from the following file: