Task Management¶
See the task loop overview for general information about the task loop.
- yosys_mau.task_loop.run_task_loop(on_run, *, handle_sigint=True)¶
Run the task loop.
- yosys_mau.task_loop.current_task()¶
Return the currently active task.
- Raises:
TaskLoopError – if no task is currently active
- Return type:
- class yosys_mau.task_loop.Task(on_run=None, *, on_prepare=None, name=None)¶
Base class for all tasks.
To customize the functionality performed by a task, either declare a subclass of
Taskoverriding the methods starting withon_or use theon_runandon_preparearguments of the constructor.The constructor creates a new task as child task of the current task and schedule it to run in the current ask loop.
- Parameters:
on_run (Callable[[Self], Awaitable[None] | None] | Callable[[], Awaitable[None] | None] | None) – The function to call when the task is run. Specifying this is an alternative to subclassing
Taskand overridingon_run.on_prepare (Callable[[Self], Awaitable[None] | None] | Callable[[], Awaitable[None] | None] | None) – The function to call when the task is prepared. Specifying this is an alternative to subclassing
Taskand overridingon_prepare.name (str | None)
- __init__(on_run=None, *, on_prepare=None, name=None)¶
The constructor creates a new task as child task of the current task and schedule it to run in the current ask loop.
- Parameters:
on_run (Callable[[Self], Awaitable[None] | None] | Callable[[], None | Awaitable[None]] | None) – The function to call when the task is run. Specifying this is an alternative to subclassing
Taskand overridingon_run.on_prepare (Callable[[Self], Awaitable[None] | None] | Callable[[], None | Awaitable[None]] | None) – The function to call when the task is prepared. Specifying this is an alternative to subclassing
Taskand overridingon_prepare.name (str | None)
- __getitem__(object)¶
Wraps the given object in a proxy that performs all attribute accesses as if they were done with this task as current task.
This is primarily intended to be used with
task_contextobjects.- Parameters:
object (T)
- Return type:
T
- property use_lease: bool¶
Whether the task should obtain a lease from the job server before running.
- property state: Literal['preparing', 'pending', 'running', 'waiting', 'done', 'cancelled', 'discarded', 'failed']¶
The current state of the task.
Todo
diagram of the possible state transitions
- property path: str¶
The path of this task in the task tree.
Lists the names of the path from the containing top-level task to this task, separated by dots.
- discard: bool¶
If set to
True, the task will be discarded (automatically cancelled) when the last of the tasks depending on it finishes (by failure or cancellation).Defaults to
True.
- restart_on_new_children: bool¶
If set to
True, new children can be added to the task even after it successfully finished. When that happens the task is restarted, i.e. its state is set topendingagain.Defaults to
False.
- property name: str¶
Name of this task.
By default the class name is used, if necessary made unique among sibling tasks by appending a number.
Subclasses can assign a more meaningful name in their constructor.
- set_error_handler(task, handler)¶
Register a handler for failing or cancelled tasks.
A registered error handler stops this task from automatically failing (or becoming cancelled) when a child or dependency fails (or is cancelled).
- Parameters:
task (Task | None) – The task to register the handler for. If
None, the handler is registered as a fallback.handler (Callable[[BaseException], None]) – The handler to call when the task fails or is cancelled. The handler is invoked as a background coroutine in the context of this task. The failed task can be recovered from the
TaskAbortedexception.
- Return type:
None
- handle_error(handler)¶
Register an error handler in the current task that handles failure or cancellation of this task.
Calling
task.handle_error(handler)is equivalent tocurrent_task().set_error_handler(task, handler).- Parameters:
handler (Callable[[BaseException], None])
- Return type:
None
- configure_task()¶
Invoked on construction with the task set as current task.
Can be used to override initialization in subclasses.
- async on_prepare()¶
Actions to perform right after the task is created, before scheduling it to run.
Can be used to add dependencies or change other task properties.
Scheduling the task is delated until this async method returns.
This executes with
selfas the current task.- Return type:
None
- async on_run()¶
Actions to perform when the task is running.
This executes with
selfas the current task.For the task to successfully finish, this async method must return, but child tasks can delay this further.
- Return type:
None
- property finished: None¶
Awaitable that resolves when the task has finished running.
This includes successful completion, cancellations and failure.
- property is_finished: bool¶
Whether the task has finished running.
This includes successful completion, cancellations and failure.
- cancel()¶
Cancel the task.
This will also cancel all pending children as well as tasks that depend on this one and do not explicitly handle cancellation of their dependencies (with the exception of the current task).
- Return type:
None
- on_cancel()¶
Actions to perform when the task is cancelled.
This runs when the task is cancelled (either directly or via a parent), but not when a dependency was cancelled. See
on_cleanupfor an alternative that runs in all cases.
- on_cleanup()¶
Actions to perform after the task finished.
This includes successful completion, cancellations and failure.
- background(target, *, wait=False, error_handler=False)¶
Run a background coroutine in the context of this task.
The coroutine will execute with the current task set to this task. When the task fails or is cancelled, the background coroutine will be cancelled as well.
- Parameters:
target (Callable[[], None | Awaitable[None]]) – The coroutine to run.
wait (bool) – Whether to wait for the background coroutine to finish before letting the task finish.
error_handler (bool) – Whether the background coroutine is an error handler. Without setting this, it is an error to install a background handler for a finished task.
- Returns:
The asyncio task (not a task loop task) that runs the background coroutine. Can be used to cancel the background coroutine.
- Return type:
Task[None]
- events(event_type, where=None)¶
Return an async iterator that yields events emitted by this task or its children.
- Parameters:
- Return type:
TaskEventStream[T_TaskEvent]
Note that using
event_typeis more efficient than awherepredicate that usesisinstance.
- sync_handle_events(event_type, handler)¶
Register a synchronous handler for events emitted by this task or its children.
The synchronous handler will be called before the
emitcall of the event returns. The handler itself runs in the context of the current task during registration.
- as_current_task()¶
Returns a context manager that temporarily overrides the current task.
This is safe to use with concurrently executing tasks as each execution context has its own current task.
- Return type:
ContextManager[None]
- block_finishing()¶
Returns a context manager that blocks the task from finishing.
This is useful in in
backgroundcoroutines that do not havewaitset, but temporarily need to prevent the task from finishing.- Return type:
Iterator[None]
- class yosys_mau.task_loop.TaskGroup(on_run=None, *, on_prepare=None, name=None)¶
A task used to group child tasks.
This is normal
Taskinitialized withdiscardset toFalseandrestart_on_new_childrenThe constructor creates a new task as child task of the current task and schedule it to run in the current ask loop.
- Parameters:
on_run (Callable[[Self], Awaitable[None] | None] | Callable[[], Awaitable[None] | None] | None) – The function to call when the task is run. Specifying this is an alternative to subclassing
Taskand overridingon_run.on_prepare (Callable[[Self], Awaitable[None] | None] | Callable[[], Awaitable[None] | None] | None) – The function to call when the task is prepared. Specifying this is an alternative to subclassing
Taskand overridingon_prepare.name (str | None)
Exceptions¶
- class yosys_mau.task_loop.TaskLoopError¶
Raised when the task loop is in an invalid state.
- class yosys_mau.task_loop.TaskAborted(task)¶
Base class for exceptions caused by a task being aborted.
This includes failure and cancellation.
- Parameters:
task (Task)
- class yosys_mau.task_loop.TaskFailed(task)¶
Bases:
TaskAbortedException caused by a task failing.
- Parameters:
task (Task)
- class yosys_mau.task_loop.TaskCancelled(task)¶
Bases:
TaskAborted,CancelledErrorException caused by a task being cancelled.
- Parameters:
task (Task)
- class yosys_mau.task_loop.DependencyAborted(task)¶
Bases:
TaskAbortedBase class for exceptions caused by a dependency being aborted.
- Parameters:
task (Task)
- class yosys_mau.task_loop.DependencyFailed(task)¶
Bases:
DependencyAborted,TaskFailedException caused by a dependency failing.
- Parameters:
task (Task)
- class yosys_mau.task_loop.DependencyCancelled(task)¶
Bases:
DependencyAborted,TaskCancelledException caused by a dependency being cancelled.
- Parameters:
task (Task)
- class yosys_mau.task_loop.ChildAborted(task)¶
Bases:
TaskAbortedBase class for exceptions caused by a child task being aborted.
- Parameters:
task (Task)
- class yosys_mau.task_loop.ChildFailed(task)¶
Bases:
ChildAborted,TaskFailedException caused by a child task failing.
- Parameters:
task (Task)
- class yosys_mau.task_loop.ChildCancelled(task)¶
Bases:
ChildAborted,TaskCancelledException caused by a child task being cancelled.
- Parameters:
task (Task)