Worker API Reference
Worker Class
Constructor
Creates a new worker instance.
Parameters:
queue_name(str): Name of the queue to processurl(str): Redis connection URLtasks(list): List of task handler functionsname(str, optional): Worker identifier
Example:
worker = Worker(
queue_name="my-queue",
url="redis://localhost:6379/0",
tasks=[my_task, another_task],
name="worker-1"
)
Methods
start
Starts the worker and begins processing tasks.
Parameters:
concurrency(int, optional): Number of concurrent tasks (default: 1)
Example:
loop
Internal method that runs the main worker loop.
Task Decorator
Constructor
Decorator for defining task handlers.
Parameters:
name(str): Unique task identifiermax_retry(int, optional): Maximum retry attemptsretry_delay(int, optional): Seconds between retries
Example:
@task(
name="process_data",
max_retry=3,
retry_delay=5
)
async def process_data(data: dict):
# Task implementation
pass
Methods
func
Returns the task's handler function.
TaskRecord Class
Internal class for task record management.
Properties
id(str): Unique task identifiername(str): Task namekwargs(Optional[str]): Serialized task argumentsdelay(Optional[Union[int, float]]): Execution delaymode(QueueAddMode): Serialization modestream_id(Optional[str]): Redis stream ID
Internal Methods
run_task
Executes a task from its record.
_run_task_func
Internal method for task function execution.
execute_tasks
Main task execution loop.
Processes delayed tasks.
cleanup_tasks_on_pel
Handles pending task cleanup.