traits_futures.base_future module¶
Base class providing common pieces of the Future machinery.
- traits_futures.base_future.ABANDONED = 'abandoned'¶
Control message sent when the callable is abandoned before execution.
- class traits_futures.base_future.BaseFuture[source]¶
Bases:
HasStrictTraits
Convenience base class for the various flavours of Future.
- cancel()[source]¶
Request cancellation of the background task.
A task in
WAITING
orEXECUTING
state will immediately be moved toCANCELLING
state. If the task is not inWAITING
orEXECUTING
state, this function does nothing.Changed in version 0.3.0: This method no longer raises for a task that isn’t cancellable. In previous versions,
RuntimeError
was raised.- Returns:
cancelled – True if the task was cancelled, False if the task was not cancellable.
- Return type:
- cancellable = Property(Bool())¶
True if cancellation of the background task can be requested, else False. Cancellation of the background task can be requested only if the future’s
state
is eitherWAITING
orEXECUTING
.
- dispatch(message)[source]¶
Dispatch a message arriving from the associated BaseTask.
This is a convenience function, and may be safely overridden by subclasses that want to use a different dispatch mechanism. For a message type
msgtype
, it looks for a method called_process_<msgtype>
and dispatches the message arguments to that method. Subclasses then only need to provide the appropriate_process_<msgtype>
methods.- Parameters:
message (
object
) – Message sent by the background task. The default implementation of this method expects the message to be in the form(message_type, message_args)
withmessage_type
a string.
- done = Property(Bool())¶
True when communications from the background task are complete. At that point, no further state changes can occur for this future. This trait has value True if the
state
is one ofCOMPLETED
,FAILED
, orCANCELLED
. It’s safe to listen to this trait for changes: it will always fire exactly once, and when it fires its value will be consistent with that of thestate
trait.
- property exception¶
Information about any exception raised by the background task.
This attribute is only available if the state of this future is
FAILED
. If the future has not reached theFAILED
state, any attempt to access this attribute will raise anAttributeError.
- Returns:
exc_info – Tuple containing exception information in string form: (exception type, exception value, formatted traceback).
- Return type:
- Raises:
AttributeError – If the task is still executing, or was cancelled, or completed without raising an exception.
- receive(message)[source]¶
Receive and process a message from the task associated to this future.
This method is primarily for use by the executors, but may also be of use in testing.
- property result¶
Result of the background task.
This attribute is only available if the state of the future is
COMPLETED
. If the future has not reached theCOMPLETED
state, any attempt to access this attribute will raise anAttributeError
.- Returns:
result – The result obtained from the background task.
- Return type:
- Raises:
AttributeError – If the task is still executing, or was cancelled, or raised an exception instead of returning a result.
- state = message¶
The state of the background task, to the best of the knowledge of this future. One of the six constants
WAITING
,EXECUTING
,COMPLETED
,FAILED
,CANCELLING
orCANCELLED
.
- class traits_futures.base_future.BaseTask[source]¶
Bases:
ABC
Mixin for background task classes, making those classes callable.
This class provides a callable wrapper allowing subclasses to easily provide a background callable task.
Subclasses should override the
run
method to customize what should happen when the task runs. This class’s__call__
implementation will take care of sending standard control messages telling the future that the task has started, completed, or raised, and delegate to therun
method for execution of the background task and sending of any custom messages.- cancelled()[source]¶
Determine whether the user has requested cancellation.
Returns True if the user has requested cancellation via the associated future’s
cancel
method, and False otherwise.- Return type:
- abstract run()[source]¶
Run the body of the background task.
- Returns:
any – May return any object. That object will be delivered to the future’s
result
attribute.- Return type:
- traits_futures.base_future.FINAL_MESSAGES = {'abandoned', 'raised', 'returned'}¶
Message types that indicate a “final” message. After a message of this type is received, no more messages will be received.
- traits_futures.base_future.RAISED = 'raised'¶
Control message sent when an exception was raised by the background callable. The argument is a tuple containing exception information.
- traits_futures.base_future.RETURNED = 'returned'¶
Control message sent to indicate that the background callable succeeded and returned a result. The argument is that result.
- traits_futures.base_future.SENT = 'sent'¶
Custom message from the future. The argument is a pair (message_type, message_args); the message type and message args are interpreted by the future.
- traits_futures.base_future.STARTED = 'started'¶
Control message sent before we start to process the target callable. The argument is always
None
.