Tasks API
Task
Task definition.
Constructor
Task(name: str, func: Callable, *, retry_policy: RetryPolicy | None = None, timeout: int | None = None, queue: str = "default", priority: int = 0)
Methods
async run(*args, **kwargs) -> Any— Run the taskcancel() -> None— Cancel the taskto_result() -> TaskResult— Convert to TaskResult
Properties
id— Task IDname— Task namestatus— Task statuscreated_at— Created timestampstarted_at— Started timestampcompleted_at— Completed timestampretry_count— Retry count
TaskStatus
Task status enum.
| Value | Description |
|---|---|
PENDING | Task is pending |
RUNNING | Task is running |
COMPLETED | Task completed |
FAILED | Task failed |
RETRY | Task will retry |
CANCELLED | Task cancelled |
TIMEOUT | Task timed out |
TaskQueue
Task queue.
Constructor
TaskQueue(name: str = "default", max_size: int = 1000)
Methods
async push(task)— Push a task to the queueasync pop(timeout)— Pop a task from the queueasync get(task_id)— Get a task by IDasync remove(task_id)— Remove a taskasync cancel(task_id)— Cancel a taskasync clear()— Clear the queueasync size()— Get the queue sizeasync pending_count()— Get pending countasync running_count()— Get running countasync completed_count()— Get completed countasync failed_count()— Get failed count
Worker
Task worker.
Constructor
Worker(registry: TaskRegistry, queue: TaskQueue | None = None, concurrency: int = 10, queue_name: str = "default")
Methods
async start()— Start the workerstop()— Stop the workeris_running()— Check if running
Scheduler
Task scheduler.
Constructor
Scheduler(queue: TaskQueue)
Methods
schedule(task, delay=None, at=None, interval=None)— Schedule a taskasync start()— Start the schedulerasync stop()— Stop the scheduler
RetryPolicy
Retry policy.
Constructor
RetryPolicy(max_retries: int = 3, delay: float = 1.0, backoff: float = 2.0, max_delay: float = 60.0, random_jitter: float = 0.1, retry_on: list[type[Exception]] | None = None)
Methods
should_retry(retry_count, error)— Check if retry should be attemptedget_delay(retry_count)— Get the delay before retry
CLI Command
Start a worker: flaxon worker app:app --concurrency 4