Source code for traits_futures.i_future

# (C) Copyright 2018-2020 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

"""
Interface for futures returned by the executor.
"""

import abc

from traits.api import Any, Bool, Event, Interface, Property, Str, Tuple

from traits_futures.future_states import FutureState


[docs]class IFuture(Interface): """ Interface for futures returned by the executor. """ #: 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`` or ``CANCELLED``. state = FutureState #: True if cancellation of the background task can be requested, #: else False. Cancellation of the background task can be requested #: only if the ``state`` is one of ``WAITING`` or ``EXECUTING``. cancellable = 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 of ``COMPLETED``, #: ``FAILED``, or ``CANCELLED``. It's safe to listen to this trait #: for changes: it will always fire exactly once, and when it fires #: it will be consistent with the ``state``. done = Property(Bool()) #: Event trait providing custom messages from the background task. #: Subclasses of ``BaseFuture`` can listen to this trait and interpret #: the messages in whatever way they like. Each message takes the #: form ``(message_type, message_args)``. message = Event(Tuple(Str(), Any())) @property @abc.abstractmethod def result(self): """ Result of the background task. This attribute is only available if the state of the future is ``COMPLETED``. If the future has not reached the ``COMPLETED`` state, any attempt to access this attribute will raise an ``AttributeError``. Returns ------- result : object The result obtained from the background task. Raises ------ AttributeError If the task is still executing, or was cancelled, or raised an exception instead of returning a result. """ @property @abc.abstractmethod def exception(self): """ 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 the ``FAILED`` state, any attempt to access this attribute will raise an ``AttributeError.`` Returns ------- exc_info : tuple(str, str, str) Tuple containing exception information in string form: (exception type, exception value, formatted traceback). Raises ------ AttributeError If the task is still executing, or was cancelled, or completed without raising an exception. """
[docs] @abc.abstractmethod def cancel(self): """ Request cancellation of the background task. A task in ``WAITING`` or ``EXECUTING`` state will immediately be moved to ``CANCELLING`` state. If the task is not in ``WAITING`` or ``EXECUTING`` state, this function will raise ``RuntimeError``. Raises ------ RuntimeError If the task has already completed or cancellation has already been requested. """