Workers¶
Workers are the main building block for asynchronous programming with aio-fluid
. They are responsible for running asynchronous tasks and managing their lifecycle.
There are several worker classes which can be imported from fluid.utils.worker
, and they aall derive from the abstract fluid.utils.worker.Worker
class.
fluid.utils.worker.WorkerState
¶
Bases: StrEnum
The state of a worker
fluid.utils.worker.Worker
¶
Bases: ABC
An Abstract Worker that can be started and stopped
All other workers derive from this class.
PARAMETER | DESCRIPTION |
---|---|
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:
|
Source code in fluid/utils/worker.py
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
¶
Called when the worker starts running
Use this function to initialize other async resources connected with the worker
on_shutdown
async
¶
called after the worker stopped running
Use this function to cleanup resources connected with the worker
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
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
abstractmethod
async
¶
run the worker
This is the only abstract method and that needs implementing. It is the coroutine that mantains the worker running.
fluid.utils.worker.WorkerFunction
¶
Bases: Worker
A Worker that runs and wait a coroutine function in a loop
PARAMETER | DESCRIPTION |
---|---|
run_function
|
The coroutine function tuo run and await at each iteration of the worker loop
TYPE:
|
heartbeat
|
The time to wait between each coroutine function run
TYPE:
|
name
|
Worker's name, if not provided it is evaluated from the class name
TYPE:
|
Source code in fluid/utils/worker.py
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
¶
Called when the worker starts running
Use this function to initialize other async resources connected with the worker
on_shutdown
async
¶
called after the worker stopped running
Use this function to cleanup resources connected with the worker
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
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
¶
fluid.utils.worker.QueueConsumer
¶
Bases: Worker
, MessageProducer[MessageType]
An Abstract Worker that can receive messages
This worker can receive messages but not consume them.
PARAMETER | DESCRIPTION |
---|---|
name
|
Worker's name, if not provided it is evaluated from the class name
TYPE:
|
Source code in fluid/utils/worker.py
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.
on_startup
async
¶
Called when the worker starts running
Use this function to initialize other async resources connected with the worker
on_shutdown
async
¶
called after the worker stopped running
Use this function to cleanup resources connected with the worker
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
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
abstractmethod
async
¶
run the worker
This is the only abstract method and that needs implementing. It is the coroutine that mantains the worker running.
get_message
async
¶
Get the next message from the queue
Source code in fluid/utils/worker.py
queue_size
¶
status
async
¶
fluid.utils.worker.QueueConsumerWorker
¶
Bases: QueueConsumer[MessageType]
A Worker that can receive and consume messages
PARAMETER | DESCRIPTION |
---|---|
on_message
|
The async callback to call when a message is received
TYPE:
|
name
|
Worker's name, if not provided it is evaluated from the class name
TYPE:
|
Source code in fluid/utils/worker.py
send
¶
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
¶
Called when the worker starts running
Use this function to initialize other async resources connected with the worker
on_shutdown
async
¶
called after the worker stopped running
Use this function to cleanup resources connected with the worker
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
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
¶
get_message
async
¶
Get the next message from the queue
Source code in fluid/utils/worker.py
queue_size
¶
fluid.utils.worker.AsyncConsumer
¶
Bases: QueueConsumer[MessageType]
A Worker that can dispatch to async callbacks
PARAMETER | DESCRIPTION |
---|---|
dispatcher
|
Async message dispatcher to dispatch messages
TYPE:
|
name
|
Worker's name, if not provided it is evaluated from the class name
TYPE:
|
Source code in fluid/utils/worker.py
send
¶
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
¶
Called when the worker starts running
Use this function to initialize other async resources connected with the worker
on_shutdown
async
¶
called after the worker stopped running
Use this function to cleanup resources connected with the worker
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
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
¶
get_message
async
¶
Get the next message from the queue
Source code in fluid/utils/worker.py
queue_size
¶
fluid.utils.worker.Workers
¶
Bases: Worker
An worker managing several workers
PARAMETER | DESCRIPTION |
---|---|
*workers
|
Workers to manage, they can also be added later via
TYPE:
|
name
|
Worker's name, if not provided it is evaluated from the class name
TYPE:
|
heartbeat
|
The time to wait between each workers status check
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:
|
Source code in fluid/utils/worker.py
has_started
¶
is_running
¶
is_stopping
¶
is_stopped
¶
after_shutdown
¶
Called after shutdown of worker
By default it does nothing, but can be overriden to do something such as exit the process.
on_startup
async
¶
Called when the worker starts running
Use this function to initialize other async resources connected with the worker
on_shutdown
async
¶
called after the worker stopped running
Use this function to cleanup resources connected with the worker
startup
async
¶
start the worker
This method creates a task to run the worker.
Source code in fluid/utils/worker.py
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
add_workers
¶
add workers to the workers
They can be added while the worker is running.