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: object

self of a bound task.

__init__(request: TaskRequest, options: TaskOptions) → None[source]
is_revoked() → bool[source]

True when the client revoked this task (cooperative cancellation).

property max_retries: int | None
property name: str
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_retries of the task.

Raises:

Retry – Always, unless the retries are exhausted, in which case exc is raised (or MaxRetriesExceededError when there is none).

update_state(task_id: str | None = None, state: str | None = None, meta: Any = None, **_ignored: Any) → None[source]

Store a custom state (e.g. PROGRESS) that the client reads with result.state and result.info.

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: object

Options 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], ...] = ()
bind: bool = False
default_retry_delay: float = 180
max_retries: int | None = 3
name: str | None = None
on_failure: Callable[[...], Any] | None = None
on_retry: Callable[[...], Any] | None = None
on_success: Callable[[...], Any] | None = None
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
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: object

What 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
args: list[Any]
callbacks: list[dict[str, Any]] | None = None
chain: list[dict[str, Any]] | None = None
chord: dict[str, Any] | None = None
delivery_info: dict[str, Any]
errbacks: list[dict[str, Any]] | None = None
eta: str | None = None
expires: str | None = None
group: str | None = None
group_index: int | None = None
hostname: str = 'celery-cloud'
id: str
is_eager: bool = False
kwargs: dict[str, Any]
parent_id: str | None = None
retries: int = 0
root_id: str | None = None
soft_time_limit: float | None = None
task: str
time_limit: float | None = 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), with retry_jitter picking 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 @task or @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: Protocol

Services the runner offers to the running task.

__init__(*args, **kwargs)
is_revoked(task_id: str) → bool[source]

True when the client asked to revoke the task.

update_state(task_id: str, state: str, meta: object) → None[source]

Store a custom state (for example PROGRESS) in the backend.

celery_cloud.runtime.get_runtime() → Runtime | None[source]

The runtime of the task being executed (None outside the runner).

celery_cloud.runtime.reset_runtime(token: Token) → None[source]
celery_cloud.runtime.set_runtime(runtime: Runtime | None) → Token[source]

Make runtime the current one; keep the token to restore the previous.