3.5 KiB
TaskQueue and QueueItem
Table of Contents
QueueItem
class QueueItem:
def __init__(self, task: Callable | Awaitable, *args, **kwargs):
A task to be executed by the TaskQueue. The task can be a callable or an awaitable. The task is wrapped as a
QueueItem object, which is then added to the TaskQueue for execution. The arguments and keyword arguments are
passed to the task when it is executed. All parameters are created as attributes of the QueueItem object.
Parameters:
| Name | Type | Description |
|---|---|---|
task |
Callable | Awaitable |
A callable or awaitable task to be executed by the TaskQueue |
args |
Any |
Positional arguments to be passed to the task when it is executed |
kwargs |
Any |
Keyword arguments to be passed to the task when it is executed |
run
def run(self) -> Any
Run the task. If the task is a coroutine, it is awaited. If the task is a callable, it is called.
TaskQueue
class TaskQueue:
def __init__(self):
Attributes:
| Name | Type | Description |
|---|---|---|
queue |
asyncio.Queue |
An asyncio.Queue queue of QueueItem objects to be executed by the TaskQueue |
add
def add(self, item: QueueItem, *args, **kwargs) -> None
Add a QueueItem to the TaskQueue queue.
Parameters:
| Name | Type | Description |
|---|---|---|
item |
QueueItem |
A QueueItem to be added to the queue |
add_task
def add_task(self, task: Callable | Awaitable, *args, **kwargs) -> None
Create a QueueItem from the task and add it to the TaskQueue queue. The task can be a callable or an awaitable.
The arguments and keyword arguments are passed to the QueueItem.
Parameters:
| Name | Type | Description |
|---|---|---|
task |
Callable | Awaitable |
A callable or awaitable task to be executed by the TaskQueue |
args |
Any |
Positional arguments to be passed to the task when it is executed |
kwargs |
Any |
Keyword arguments to be passed to the task when it is executed |
worker
async def worker(self) -> None
A worker that processes the QueueItem objects in the TaskQueue queue. The worker runs indefinitely, processing
QueueItem objects as they are added to the queue.
start
def start(self) -> None
Start the worker that processes the QueueItem objects in the TaskQueue queue.