你正在阅读 Celery 3.1 的文档。开发版本文档见: 此处.
Task Implementation: Task request context, and the base task class.
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.
参数: | task_id – Task id to get result for. |
---|
Defines how and when task error e-mails should be sent.
参数: | 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 nodename.
Return 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.
Execution strategy used, or the qualified name of one.
If True the task is an abstract base class.
If disabled the worker will not forward magic keyword arguments. Deprecated and scheduled for removal in v4.0.
When enabled messages for this task will be acknowledged after the task has been executed, and not just before which is the default behavior.
Please note that this means the task may be executed twice if the worker crashes mid execution (which may be acceptable for some applications).
The application default can be overridden with the CELERY_ACKS_LATE setting.
Handler called after the task returns.
参数: |
|
---|
The return value of this handler is ignored.
Execute this task locally, by blocking until the task returns.
参数: |
|
---|
:rtype celery.result.EagerResult:
Apply tasks asynchronously by sending a message.
参数: |
|
---|
Also supports all keyword arguments supported by kombu.Producer.publish().
注解
If the CELERY_ALWAYS_EAGER setting is set, it will be replaced by a local apply() call instead.
If disabled this task won’t be registered automatically.
The result store backend used for this task.
Default time in seconds before a retry of the task should be executed. 3 minutes by default.
Star argument version of apply_async().
Does not support the extra options enabled by apply_async().
参数: |
|
---|
:returns celery.result.AsyncResult:
Default task expiry time.
If enabled the worker will not store task state and return values for this task. Defaults to the CELERY_IGNORE_RESULT setting.
Maximum number of retries before giving up. If set to None, it will never stop retrying.
Name of the task.
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.
参数: |
|
---|
The return value of this handler is ignored.
Retry handler.
This is run by the worker when the task is to be retried.
参数: |
|
---|
The return value of this handler is ignored.
Success handler.
Run by the worker if the task executes successfully.
参数: |
|
---|
The return value of this handler is ignored.
Rate limit for this task type. Examples: None (no rate limit), ‘100/s’ (hundred tasks a second), ‘100/m’ (hundred tasks a minute),`‘100/h’` (hundred tasks an hour)
Get current request object.
Retry the task.
参数: |
|
---|---|
引发 celery.exceptions.Retry: | |
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
>>> from imaginary_twitter_lib import Twitter
>>> from proj.celery import app
>>> @app.task()
... def tweet(auth, message):
... twitter = Twitter(oauth=auth)
... try:
... twitter.post_status_update(message)
... except twitter.FailWhale as exc:
... # Retry in 5 minutes.
... raise 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 raise in front of the retry to convey that the rest of the block will not be executed.
If enabled an email will be sent to ADMINS whenever a task of this type fails.
The name of a serializer that are registered with kombu.serialization.registry. Default is ‘pickle’.
Soft time limit. Defaults to the CELERYD_TASK_SOFT_TIME_LIMIT setting.
When enabled errors will be stored even if the task is otherwise configured to ignore results.
Return signature object for this task, wrapping arguments and execution options for a single task invocation.
List/tuple of expected exceptions.
These are errors that are expected in normal operation and that should not be regarded as a real error by the worker. Currently this means that the state will be updated to an error state, but the worker will not log the event as an error.
Hard time limit. Defaults to the CELERYD_TASK_TIME_LIMIT setting.
If enabled the task will report its status as ‘started’ when the task is executed by a worker. Disabled by default as the normal behaviour is to not report that level of granularity. Tasks are either pending, finished, or waiting to be retried.
Having a ‘started’ status can be useful for when there are long running tasks and there is a need to report which task is currently running.
The application default can be overridden using the CELERY_TRACK_STARTED setting.
If enabled the request will keep track of subtasks started by this task, and this information will be sent with the result (result.children).