module documentation

Scheduling utility methods and classes.

Class Clock Provide a deterministic, easily-controlled implementation of IReactorTime.callLater. This is commonly useful for writing deterministic unit tests for code which schedules events using this API.
Class CooperativeTask A CooperativeTask is a task object inside a Cooperator, which can be paused, resumed, and stopped. It can also have its completion (or termination) monitored.
Class Cooperator Cooperative task scheduler.
Class LoopingCall Call a function repeatedly.
Exception NotPaused This exception is raised when a task is resumed which was not previously paused.
Exception SchedulerError The operation could not be completed because the scheduler or one of its tasks was in an invalid state. This exception should not be raised directly, but is a superclass of various scheduler-state-related exceptions.
Exception SchedulerStopped The operation could not complete because the scheduler was stopped in progress or was already stopped.
Exception TaskDone The operation could not complete because the task was already completed.
Exception TaskFailed The operation could not complete because the task died with an unhandled error.
Exception TaskFinished The operation could not complete because the task was already completed, stopped, encountered an error or otherwise permanently stopped running.
Exception TaskStopped The operation could not complete because the task was stopped.
Function coiterate Cooperatively iterate over the given iterator, dividing runtime between it and all other iterators which have been passed to this function and not yet exhausted.
Function cooperate Start running the given iterator as a long-running cooperative task, by calling next() on it as a periodic timed event.
Function deferLater Call the given function after a certain period of time has passed.
Function react Call main and run the reactor until the Deferred it returns fires or the coroutine it returns completes.
Class _Timer Undocumented
Function _defaultScheduler Undocumented
Constant _EPSILON Undocumented
Type Variable _T Undocumented
Type Variable _TaskResultT Undocumented
Variable _theCooperator Undocumented
def coiterate(iterator: Iterator[_T]) -> Deferred[Iterator[_T]]: (source)

Cooperatively iterate over the given iterator, dividing runtime between it and all other iterators which have been passed to this function and not yet exhausted.

Parameters
iterator:Iterator[_T]the iterator to invoke.
Returns
Deferred[Iterator[_T]]a Deferred that will fire when the iterator finishes.
def cooperate(iterator: Iterator[_T]) -> CooperativeTask: (source)

Start running the given iterator as a long-running cooperative task, by calling next() on it as a periodic timed event.

This is very useful if you have computationally expensive tasks that you want to run without blocking the reactor. Just break each task up so that it yields frequently, pass it in here and the global Cooperator will make sure work is distributed between them without blocking longer than a single iteration of a single task.

Parameters
iterator:Iterator[_T]the iterator to invoke.
Returns
CooperativeTaska CooperativeTask object representing this task.
def deferLater(clock: IReactorTime, delay: float, callable: Callable[..., _T] | None = None, *args: object, **kw: object) -> Deferred[_T]: (source)

Call the given function after a certain period of time has passed.

Parameters
clock:IReactorTimeThe object which will be used to schedule the delayed call.
delay:floatThe number of seconds to wait before calling the function.
callable:Callable[..., _T] | NoneThe callable to call after the delay, or None.
*args:objectThe positional arguments to pass to callable.
**kw:objectThe keyword arguments to pass to callable.
Returns
Deferred[_T]A deferred that fires with the result of the callable when the specified time has elapsed.
def react(main: Callable[..., Deferred[_T] | Coroutine[Deferred[_T], object, _T]], argv: Iterable[object] = (), _reactor: IReactorCore | None = None) -> NoReturn: (source)

Call main and run the reactor until the Deferred it returns fires or the coroutine it returns completes.

This is intended as the way to start up an application with a well-defined completion condition. Use it to write clients or one-off asynchronous operations. Prefer this to calling reactor.run directly, as this function will also:

  • Take care to call reactor.stop once and only once, and at the right time.
  • Log any failures from the Deferred returned by main.
  • Exit the application when done, with exit code 0 in case of success and 1 in case of failure. If main fails with a SystemExit error, the code returned is used.

The following demonstrates the signature of a main function which can be used with react:

  async def main(reactor, username, password):
      return "ok"

  task.react(main, ("alice", "secret"))
Parameters
main:Callable[..., Deferred[_T] | Coroutine[Deferred[_T], object, _T]]A callable which returns a Deferred or coroutine. It should take the reactor as its first parameter, followed by the elements of argv.
argv:Iterable[object]A list of arguments to pass to main. If omitted the callable will be invoked with no additional arguments.
_reactor:IReactorCore | NoneAn implementation detail to allow easier unit testing. Do not supply this parameter.
Returns
NoReturnUndocumented
Present Since
12.3
def _defaultScheduler(callable: Callable[[], None]) -> IDelayedCall: (source)

Undocumented

Undocumented

Value
1e-08

Undocumented

Value
TypeVar('_T')
_TaskResultT = (source)

Undocumented

Value
TypeVar('_TaskResultT')
_theCooperator = (source)

Undocumented