Task
A Task is a unit of work that can be scheduled by the
Scheduler.
It is defined by its function= to call. Whenever a Task
has its submit() method called,
the function will be dispatched to run by a Scheduler.
When a task has returned, either successfully, or with an exception,
it will emit @events to indicate so. You can subscribe to these events
with callbacks and act accordingly.
@events
Check out the @events reference
for more on how to customize these callbacks. You can also take a look
at the API of on() for more information.
An event that is emitted when a future is submitted to the scheduler. It will pass the future as the first argument with args and kwargs following.
This is done before any callbacks are attached to the future.
Usage
The usual way to create a task is with
Scheduler.task(),
where you provide the function= to call.
from amltk import Scheduler
def f(x: int) -> int:
return x * 2
scheduler = Scheduler.with_processes(2)
task = scheduler.task(f)
@scheduler.on_start
def on_start():
task.submit(1)
@task.on_result
def on_result(future: Future[int], result: int):
print(f"Task {future} returned {result}")
scheduler.run()
Task
╭─ Scheduler ──────────────────────────────────────────────────────────────────╮
│ Executor Queue: (0) │
│ ╭─ ProcessPoolExecutor─╮ │
│ │ {'max_workers': 2} │ │
│ ╰──────────────────────╯ │
│ │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────╯
┣━━ @on_empty 1
┃ @on_start 1
┃ └── def on_start() (1)
┃ @on_finishing 1
┃ @on_finished 1
┃ @on_stop
┃ @on_timeout
┃ @on_future_submitted 1
┃ @on_future_done 1
┃ @on_future_cancelled
┃ @on_future_exception
┃ @on_future_result 1
┗━━ ╭─ Task f(x: 'int') -> 'int' ──────────────────────────────────────────────╮
│ @on_submitted 1 │
│ @on_done 1 │
│ @on_result 1 │
│ └── def on_result(future: 'Future[int]', result: 'int') (1) │
│ @on_exception │
│ @on_cancelled │
╰───────────────────────────── Ref: qJ1yylZq ──────────────────────────────╯
If you'd like to simply just call the original function, without submitting it to
the scheduler, you can always just call the task directly, i.e. task(1).
You can also provide Plugins to the task,
to modify tasks, add functionality and add new events.
class Task(function, scheduler, *, plugins=(), init_plugins=True)
#
Bases: RichRenderable, Generic[P, R]
The task class.
| PARAMETER | DESCRIPTION |
|---|---|
function |
The function of this task
TYPE:
|
scheduler |
The scheduler that this task is registered with.
TYPE:
|
plugins |
The plugins to use for this task. |
init_plugins |
Whether to initialize the plugins or not.
TYPE:
|
Source code in src/amltk/scheduling/task.py
unique_ref: str
attr
#
A unique reference to this task.
emitter: Emitter
attr
#
The emitter for events of this task.
plugins: list[Plugin]
attr
#
The plugins to use for this task.
function: Callable[P, R]
attr
#
The function of this task
scheduler: Scheduler
attr
#
The scheduler that this task is registered with.
init_plugins: bool
attr
#
Whether to initialize the plugins or not.
queue: list[Future[R]]
attr
#
The queue of futures for this task.
on_submitted: Subscriber[Concatenate[Future[R], P]]
attr
#
An event that is emitted when a future is submitted to the scheduler. It will pass the future as the first argument with args and kwargs following.
This is done before any callbacks are attached to the future.
on_done: Subscriber[Future[R]]
attr
#
on_result: Subscriber[Future[R], R]
attr
#
on_exception: Subscriber[Future[R], BaseException]
attr
#
on_cancelled: Subscriber[Future[R]]
attr
#
n_running: int
prop
#
Get the number of futures for this task that are currently running.
def futures()
#
def on(event, callback=None, *, when=None, max_calls=None, repeat=1, every=1, hidden=False)
#
Subscribe to an event.
| PARAMETER | DESCRIPTION |
|---|---|
event |
The event to subscribe to. |
callback |
The callback to call when the event is emitted. If not specified, what is returned can be used as a decorator. |
when |
A predicate to determine whether to call the callback. |
max_calls |
The maximum number of times to call the callback.
TYPE:
|
repeat |
The number of times to repeat the subscription.
TYPE:
|
every |
The number of times to wait between repeats.
TYPE:
|
hidden |
Whether to hide the callback in visual output. This is mainly used to facilitate Plugins who act upon events but don't want to be seen, primarily as they are just book-keeping callbacks.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Subscriber[P2] | Subscriber[...] | None
|
The subscriber if no callback was provided, otherwise |
Source code in src/amltk/scheduling/task.py
def running()
#
def submit(*args, **kwargs)
#
Dispatch this task.
| PARAMETER | DESCRIPTION |
|---|---|
*args |
The positional arguments to pass to the task.
TYPE:
|
**kwargs |
The keyword arguments to call the task with.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Future[R] | None
|
The future of the task, or |
Future[R] | None
|
some max call limit or a plugin prevented if from being submitted. |
| RAISES | DESCRIPTION |
|---|---|
SchedulerNotRunningError
|
If the scheduler is not running.
You can protect against this using,
|
Source code in src/amltk/scheduling/task.py
def copy(*, init_plugins=True)
#
Create a copy of this task.
Will use the same scheduler and function, but will have a different event manager such that any events listend to on the old task will not trigger with the copied task.
| PARAMETER | DESCRIPTION |
|---|---|
init_plugins |
Whether to initialize the copied plugins on the copied
task. Usually you will want to leave this as
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
A copy of this task. |