Task API
from celery_cloud import task, Retry, Ignore, Reject (see Server: the Celery-like task API).
Celery-like API for local tasks (task://module/function).
The original contract keeps working: a plain function f(args, kwargs) receives the
list of positional arguments and the dict of keyword arguments. Decorating a function
with task() switches it to the natural calling convention and adds what Celery’s
@app.task offers: bind, self.request, self.retry(),
self.update_state(), autoretry_for, backoff and time limits:
from celery_cloud import task
@task(bind=True, max_retries=3, default_retry_delay=30)
def fetch(self, url: str) -> str:
try:
return download(url)
except ConnectionError as exc:
raise self.retry(exc=exc)
- class celery_cloud.tasks.TaskContext(request: TaskRequest, options: TaskOptions)[source]
Bases:
objectselfof a bound task.- __init__(request: TaskRequest, options: TaskOptions) None[source]
- retry(exc: BaseException | None = None, countdown: float | None = None, eta: datetime | None = None, max_retries: int | None = None, **_ignored: Any) NoReturn[source]
Ask the runner to run this task again later (use as
raise self.retry()).- Parameters:
exc – The exception that caused the retry; it is the failure reported when the retries run out.
countdown – Seconds to wait (default:
default_retry_delay).eta – Absolute time of the next attempt; wins over
countdown.max_retries – Overrides the
max_retriesof the task.
- Raises:
Retry – Always, unless the retries are exhausted, in which case
excis raised (orMaxRetriesExceededErrorwhen there is none).
- class celery_cloud.tasks.TaskOptions(name: str | None = None, bind: bool = False, max_retries: int | None = 3, default_retry_delay: float = 180, autoretry_for: tuple[type[BaseException], ...] = (), retry_backoff: bool | float = False, retry_backoff_max: float = 600, retry_jitter: bool = True, soft_time_limit: float | None = None, time_limit: float | None = None, on_success: Callable[[...], Any] | None = None, on_failure: Callable[[...], Any] | None = None, on_retry: Callable[[...], Any] | None = None)[source]
Bases:
objectOptions of a decorated task (same names and defaults as Celery).
- __init__(name: str | None = None, bind: bool = False, max_retries: int | None = 3, default_retry_delay: float = 180, autoretry_for: tuple[type[BaseException], ...] = (), retry_backoff: bool | float = False, retry_backoff_max: float = 600, retry_jitter: bool = True, soft_time_limit: float | None = None, time_limit: float | None = None, on_success: Callable[[...], Any] | None = None, on_failure: Callable[[...], Any] | None = None, on_retry: Callable[[...], Any] | None = None) None
- autoretry_for: tuple[type[BaseException], ...] = ()
- class celery_cloud.tasks.TaskRequest(id: str, task: str, args: list[Any] = <factory>, kwargs: dict[str, ~typing.Any]=<factory>, retries: int = 0, eta: str | None = None, expires: str | None = None, parent_id: str | None = None, root_id: str | None = None, group: str | None = None, group_index: int | None = None, hostname: str = 'celery-cloud', delivery_info: dict[str, ~typing.Any]=<factory>, callbacks: list[dict[str, ~typing.Any]] | None=None, errbacks: list[dict[str, ~typing.Any]] | None=None, chain: list[dict[str, ~typing.Any]] | None=None, chord: dict[str, ~typing.Any] | None=None, time_limit: float | None = None, soft_time_limit: float | None = None, is_eager: bool = False)[source]
Bases:
objectWhat the task knows about the message that is being executed.
- __init__(id: str, task: str, args: list[Any] = <factory>, kwargs: dict[str, ~typing.Any]=<factory>, retries: int = 0, eta: str | None = None, expires: str | None = None, parent_id: str | None = None, root_id: str | None = None, group: str | None = None, group_index: int | None = None, hostname: str = 'celery-cloud', delivery_info: dict[str, ~typing.Any]=<factory>, callbacks: list[dict[str, ~typing.Any]] | None=None, errbacks: list[dict[str, ~typing.Any]] | None=None, chain: list[dict[str, ~typing.Any]] | None=None, chord: dict[str, ~typing.Any] | None=None, time_limit: float | None = None, soft_time_limit: float | None = None, is_eager: bool = False) None
- celery_cloud.tasks.backoff_countdown(options: TaskOptions, retries: int) float[source]
Seconds to wait before retry number
retries + 1(exponential backoff).Same formula as Celery:
min(retry_backoff_max, factor * 2**retries), withretry_jitterpicking a random value between 0 and that.
- celery_cloud.tasks.call_task_function(function: Callable[[...], Any], request: TaskRequest) Any[source]
Call a task function with the convention it was written for.
- celery_cloud.tasks.get_options(function: Callable[[...], Any]) TaskOptions | None[source]
The options of a decorated task, None for a plain
f(args, kwargs).
- celery_cloud.tasks.task(func: F | None = None, /, **options: Any) Any[source]
Decorator that turns a function into a Celery-like local task.
Usable as
@taskor@task(bind=True, max_retries=5). The function stays directly callable (handy in unit tests).
Runtime
The services a running task can reach (custom states, revocation), published by the runner for the duration of the task.
What task code can reach while the runner executes it.
The runner publishes a Runtime for the duration of a task, and TaskContext
(self of a bound task) talks to it. Keeping this in a context variable lets task
code stay independent from the backend and from AWS.
- class celery_cloud.runtime.Runtime(*args, **kwargs)[source]
Bases:
ProtocolServices the runner offers to the running task.
- __init__(*args, **kwargs)