This document is for Celery's development version, which can be significantly different from previous releases. Get old docs here: 2.5.
The task implementation has been moved to celery.app.task.
This contains the backward compatible Task class used in the old API.
| copyright: |
|
|---|---|
| license: | BSD, see LICENSE for more details. |
Task base class.
When called tasks apply the run() method. This method must be defined by all tasks (that is unless the __call__() method is overridden).
Get AsyncResult instance for this kind of task.
| Parameters: | task_id – Task id to get result for. |
|---|
Defines how and when task error e-mails should be sent.
| Parameters: | task – The task instance that raised the error. |
|---|
subject and body are format strings which are passed a context containing the following keys:
name
Name of the task.
id
UUID of the task.
exc
String representation of the exception.
args
Positional arguments.
kwargs
Keyword arguments.
traceback
String representation of the traceback.
hostname
Worker hostname.
Returns true or false depending on if a task error mail should be sent for this type of error.
The tasks max restart limit has been exceeded.
Handler called after the task returns.
| Parameters: |
|
|---|
The return value of this handler is ignored.
Execute this task locally, by blocking until the task returns.
| Parameters: |
|
|---|
:rtype celery.result.EagerResult:
Apply tasks asynchronously by sending a message.
| Parameters: |
|
|---|
Note
If the CELERY_ALWAYS_EAGER setting is set, it will be replaced by a local apply() call instead.
Star argument version of apply_async().
Does not support the extra options enabled by apply_async().
| Parameters: |
|
|---|
:returns celery.result.AsyncResult:
Establish a connection to the message broker.
The method the worker calls to execute the task.
| Parameters: |
|---|
Get message consumer.
:rtype kombu.messaging.Consumer:
Warning
If you don’t specify a connection, one will automatically be established for you, in that case you need to close this connection after use:
>>> consumer = self.get_consumer()
>>> # do something with consumer
>>> consumer.close()
>>> consumer.connection.close()
Get task-aware logger object.
Get a celery task message publisher.
:rtype TaskProducer:
Warning
If you don’t specify a connection, one will automatically be established for you, in that case you need to close this connection after use:
>>> publisher = self.get_publisher()
>>> # ... do something with publisher
>>> publisher.connection.close()
This method can be defined to do additional actions when the task class is bound to an app.
Error handler.
This is run by the worker when the task fails.
| Parameters: |
|
|---|
The return value of this handler is ignored.
Retry handler.
This is run by the worker when the task is to be retried.
| Parameters: |
|
|---|
The return value of this handler is ignored.
Success handler.
Run by the worker if the task executes successfully.
| Parameters: |
|
|---|
The return value of this handler is ignored.
Retry the task.
| Parameters: |
|
|---|---|
| Raises celery.exceptions.RetryTaskError: | |
To tell the worker that the task has been re-sent for retry. This always happens, unless the throw keyword argument has been explicitly set to False, and is considered normal operation. |
|
Example
>>> @task
>>> def tweet(auth, message):
... twitter = Twitter(oauth=auth)
... try:
... twitter.post_status_update(message)
... except twitter.FailWhale, exc:
... # Retry in 5 minutes.
... return tweet.retry(countdown=60 * 5, exc=exc)
Although the task will never return above as retry raises an exception to notify the worker, we use return in front of the retry to convey that the rest of the block will not be executed.
The body of the task executed by workers.
.s(*a, **k) -> .subtask(a, k)
Returns subtask object for this task, wrapping arguments and execution options for a single task invocation.
A periodic task is a task that adds itself to the CELERYBEAT_SCHEDULE setting.
Meta class for tasks.
Automatically registers the task in the task registry, except if the abstract attribute is set.
If no name attribute is provided, then no name is automatically set to the name of the module it was defined in, and the class name.