Task Consumer¶
The task consumer is a TaskManager which is also a Workers that consumes tasks from the task queue and executes them. It can be imported from fluid.scheduler:
fluid.scheduler.TaskConsumer
¶
Bases: TaskManager, Workers
The Task Consumer is a Task Manager responsible for consuming tasks from a task queue
Create the task consumer and the workers it runs.
Workers added via add_workers are started by startup, therefore
they only run in a process which consumes the task queue.
The async dispatcher is added as an async context manager instead, so
it is started by __aenter__ and runs in every process which uses the
task manager. A cpu bound task runs in a process of its own, which
executes a single task and never starts the consumer workers, and it
still needs to dispatch the lifecycle events its plugins and handlers
subscribe to.
| PARAMETER | DESCRIPTION |
|---|---|
deps
|
Application dependencies available to every task run. See the Task Dependencies tutorial.
TYPE:
|
config
|
Task manager configuration. Built from the extra keyword arguments when not provided.
TYPE:
|
name
|
Worker's name, if not provided it is evaluated from the class name
TYPE:
|
stopping_grace_period
|
Grace period in seconds to wait for workers to stop running when this worker is shutdown. It defaults to the
TYPE:
|
**kwargs
|
Configuration fields, used when
TYPE:
|
Source code in fluid/scheduler/consumer.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
deps
instance-attribute
¶
Dependencies for the task manager.
Production applications requires global dependencies to be
available to all tasks. This can be achieved by setting
the deps attribute of the task manager to an object
with the required dependencies.
Each task can cast the dependencies to the required type.
state
instance-attribute
¶
State for the task manager. This can be used by plugins to store state in the task manager.
config
instance-attribute
¶
config = config or TaskManagerConfig(**kwargs)
Task manager configuration
dispatcher
instance-attribute
¶
dispatcher = TaskDispatcher()
A dispatcher of TaskRun events.
Application can register handlers to listen for events happening during the lifecycle of a task run.
has_started
¶
is_running
¶
is_stopping
¶
is_stopped
¶
gracefully_stop
¶
after_shutdown
¶
Called after shutdown of worker
By default it does nothing, but can be overriden to do something such as exit the process.
status
async
¶
on_startup
async
¶
on_shutdown
async
¶
shutdown
async
¶
Shutdown a running worker and wait for it to stop
This method will try to gracefully stop the worker and wait for it to stop. If the worker does not stop in the grace period, it will force shutdown by cancelling the task.
Source code in fluid/utils/worker.py
wait_for_shutdown
async
¶
Wait for the worker to stop
This method will wait for the worker to stop running, but doesn't try to gracefully stop it nor force shutdown.
Source code in fluid/utils/worker.py
workers
¶
run
async
¶
Source code in fluid/utils/worker.py
add_workers
¶
add workers to the workers
They can be added while the worker is running.
add_async_context_manager
¶
Add an async context manager to the task manager
These context managers are entered when the task manager starts
register_task
¶
Register a task with the task manager
| PARAMETER | DESCRIPTION |
|---|---|
task
|
Task to register
TYPE:
|
tags
|
Extra tags to add to the task before registering it
TYPE:
|
Source code in fluid/scheduler/consumer.py
execute
async
¶
Execute a task and wait for it to finish
This method is an async method that should be used in an asynchronous context when one need to wait for the task to finish execution.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
execute_sync
¶
Execute a task synchronously
This method is a blocking method that should be used in a synchronous context.
| PARAMETER | DESCRIPTION |
|---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
queue
async
¶
Queue a task for execution
This methods fires two events:
init: when the task run is createdqueued: after the task is queued
It returns the TaskRun object
| PARAMETER | DESCRIPTION |
|---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
from_task_run
|
The task run queueing this one, if any. Prefer TaskRun.queue, which passes it for you.
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
create_task_run
¶
Create a TaskRun in init state
| PARAMETER | DESCRIPTION |
|---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
run_id
|
Unique ID for the task run. If not provided a new UUID is generated.
TYPE:
|
priority
|
Override the default task priority if provided
TYPE:
|
from_task_run
|
The task run creating this one, if any. It records the chain.
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|
Source code in fluid/scheduler/consumer.py
register_from_module
¶
Register tasks from a python module
| PARAMETER | DESCRIPTION |
|---|---|
module
|
Python module with tasks implementations - can contain any object, only instances of Task are registered
TYPE:
|
tags
|
Extra tags to add to every registered task
TYPE:
|
Source code in fluid/scheduler/consumer.py
register_from_dict
¶
Register tasks from a python dictionary
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Python dictionary with tasks implementations - can contain any object, only instances of Task are registered
TYPE:
|
tags
|
Extra tags to add to every registered task
TYPE:
|
Source code in fluid/scheduler/consumer.py
with_plugin
¶
Register a plugin with the task manager
| PARAMETER | DESCRIPTION |
|---|---|
plugin
|
The plugin to register
TYPE:
|
startup
async
¶
Start the task consumer workers.
A cpu bound process executes a single task and exits, it never consumes
the queue. Reaching this point means the entry point ignored the exec
command, so it cannot run cpu bound tasks.
Source code in fluid/scheduler/consumer.py
sync_queue
¶
queue_and_wait
async
¶
Queue a task and wait for it to finish
| PARAMETER | DESCRIPTION |
|---|---|
task
|
The task or task name, if a task name it must be registered with the task manager.
TYPE:
|
timeout
|
Timeout for waiting the task to finish
TYPE:
|
**params
|
The optional parameters for the task run. They must match the task params model
TYPE:
|