Executor

This module provides for asynchronous execution of tasks.

class TaskExecutor(max_workers: int | None = 1)[source]

An asynchronous executor of tasks.

get_input_queue_size() int[source]

Expose the number of queued items in the ThreadPoolExecutor.

Returns:

Number of commands in the input queue

submit(func: Callable[[...], None], args: Any | None = None, kwargs: Any | None = None, is_cmd_allowed: Callable[[], bool] | None = None, task_callback: TaskCallbackType | None = None) tuple[TaskStatus, str][source]

Submit a new task.

Parameters:
  • func – the function to be executed.

  • args – positional arguments to the function

  • kwargs – keyword arguments to the function

  • is_cmd_allowed – sanity check for func

  • task_callback – the callback to be called when the status or progress of the task execution changes

Returns:

(TaskStatus, message)

abort(task_callback: TaskCallbackType | None = None) tuple[TaskStatus, str][source]

Tell this executor to abort execution.

New submissions will be rejected until the queue is empty and no tasks are still running. Tasks on the queue will be marked as aborted and not run. Tasks already running will be allowed to continue running

Parameters:

task_callback – callback for abort complete

Returns:

tuple of task status & message

class TaskStatus(value)[source]

The status of a task.

A task is any operation that is being performed asynchronously.

STAGING = 0

The request to execute the task has not yet been acted upon.

QUEUED = 1

The task has been accepted and will be executed at a future time.

IN_PROGRESS = 2

The task is being executed.

ABORTED = 3

The task has been aborted.

NOT_FOUND = 4

The task is not found.

COMPLETED = 5

The task was completed.

Note that this does not necessarily imply that the task was executed successfully. Whether the task succeeded or failed is a matter for the ResultCode. The COMPLETED value indicates only that execution of the task ran to completion.

REJECTED = 6

The task was rejected.

FAILED = 7

The task failed to complete.

Note that this should not be used for a task that executes to completion, but does not achieve its goal. This kind of domain-specific notion of “succeeded” versus “failed” should be passed in a ResultCode. Here, FAILED means that the task executor has detected a failure of the task to run to completion. For example, execution of the task might have resulted in the raising of an uncaught exception.