This document describes Celery 2.4. For development docs, go here.
celery.app.task¶
celery.app.task¶
Tasks Implementation.
copyright: |
|
---|---|
license: | BSD, see LICENSE for more details. |
-
class
celery.app.task.
TaskType
¶ 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.
-
class
celery.app.task.
BaseTask
¶ 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).-
classmethod
AsyncResult
(task_id)¶ Get AsyncResult instance for this kind of task.
Parameters: task_id – Task id to get result for.
-
class
ErrorMail
(task, **kwargs)¶ Defines how and when task error e-mails should be sent.
Parameters: task – The task instance that raised the error. subject
andbody
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.
-
should_send
(context, exc)¶ Returns true or false depending on if a task error mail should be sent for this type of error.
-
exception
BaseTask.
MaxRetriesExceededError
¶ The tasks max restart limit has been exceeded.
-
BaseTask.
abstract
= None¶ If
True
the task is an abstract base class.
-
BaseTask.
accept_magic_kwargs
= False¶ If disabled the worker will not forward magic keyword arguments. Deprecated and scheduled for removal in v3.0.
-
BaseTask.
acks_late
= False¶ 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.
-
BaseTask.
after_return
(status, retval, task_id, args, kwargs, einfo)¶ Handler called after the task returns.
Parameters: - status – Current task state.
- retval – Task return value/exception.
- task_id – Unique id of the task.
- args – Original arguments for the task that failed.
- kwargs – Original keyword arguments for the task that failed.
- einfo –
ExceptionInfo
instance, containing the traceback (if any).
The return value of this handler is ignored.
-
BaseTask.
app
= None¶ The application instance associated with this task class.
-
classmethod
BaseTask.
apply
(args=None, kwargs=None, **options)¶ Execute this task locally, by blocking until the task returns.
Parameters: - args – positional arguments passed on to the task.
- kwargs – keyword arguments passed on to the task.
- throw – Re-raise task exceptions. Defaults to
the
CELERY_EAGER_PROPAGATES_EXCEPTIONS
setting.
:rtype
celery.result.EagerResult
:
-
classmethod
BaseTask.
apply_async
(args=None, kwargs=None, countdown=None, eta=None, task_id=None, publisher=None, connection=None, connect_timeout=None, router=None, expires=None, queues=None, **options)¶ Apply tasks asynchronously by sending a message.
Parameters: - args – The positional arguments to pass on to the
task (a
list
ortuple
). - kwargs – The keyword arguments to pass on to the
task (a
dict
) - countdown – Number of seconds into the future that the task should execute. Defaults to immediate execution (do not confuse with the immediate flag, as they are unrelated).
- eta – A
datetime
object describing the absolute time and date of when the task should be executed. May not be specified if countdown is also supplied. (Do not confuse this with the immediate flag, as they are unrelated). - expires – Either a
int
, describing the number of seconds, or adatetime
object that describes the absolute time and date of when the task should expire. The task will not be executed after the expiration time. - connection – Re-use existing broker connection instead of establishing a new one. The connect_timeout argument is not respected if this is set.
- connect_timeout – The timeout in seconds, before we give up on establishing a connection to the AMQP server.
- retry – If enabled sending of the task message will be retried
in the event of connection loss or failure. Default
is taken from the
CELERY_TASK_PUBLISH_RETRY
setting. Note you need to handle the publisher/connection manually for this to work. - retry_policy – Override the retry policy used. See the
CELERY_TASK_PUBLISH_RETRY
setting. - routing_key – The routing key used to route the task to a
worker server. Defaults to the
routing_key
attribute. - exchange – The named exchange to send the task to.
Defaults to the
exchange
attribute. - exchange_type – The exchange type to initialize the exchange
if not already declared. Defaults to the
exchange_type
attribute. - immediate – Request immediate delivery. Will raise an
exception if the task cannot be routed to a worker
immediately. (Do not confuse this parameter with
the countdown and eta settings, as they are
unrelated). Defaults to the
immediate
attribute. - mandatory – Mandatory routing. Raises an exception if
there’s no running workers able to take on this
task. Defaults to the
mandatory
attribute. - priority – The task priority, a number between 0 and 9.
Defaults to the
priority
attribute. - serializer – A string identifying the default
serialization method to use. Can be pickle,
json, yaml, msgpack or any custom
serialization method that has been registered
with
kombu.serialization.registry
. Defaults to theserializer
attribute. - compression – A string identifying the compression method
to use. Can be one of
zlib
,bzip2
, or any custom compression methods registered withkombu.compression.register()
. Defaults to theCELERY_MESSAGE_COMPRESSION
setting.
Note
If the
CELERY_ALWAYS_EAGER
setting is set, it will be replaced by a localapply()
call instead.- args – The positional arguments to pass on to the
task (a
-
BaseTask.
autoregister
= True¶ If disabled this task won’t be registered automatically.
-
BaseTask.
backend
= None¶ The result store backend used for this task.
-
BaseTask.
default_retry_delay
= 180¶ Default time in seconds before a retry of the task should be executed. 3 minutes by default.
-
classmethod
BaseTask.
delay
(*args, **kwargs)¶ Star argument version of
apply_async()
.Does not support the extra options enabled by
apply_async()
.Parameters: - *args – positional arguments passed on to the task.
- **kwargs – keyword arguments passed on to the task.
:returns
celery.result.AsyncResult
:
-
BaseTask.
delivery_mode
= None¶ Override the apps default delivery mode for this task. Default is “persistent”, but you can change this to “transient”, which means messages will be lost if the broker is restarted. Consult your broker manual for any additional delivery modes.
-
BaseTask.
error_whitelist
= ()¶ List of exception types to send error emails for.
-
classmethod
BaseTask.
establish_connection
(connect_timeout=None)¶ Establish a connection to the message broker.
-
BaseTask.
exchange
= None¶ Overrides the apps default exchange for this task.
-
BaseTask.
exchange_type
= None¶ Overrides the apps default exchange type for this task.
-
BaseTask.
execute
(request, pool, loglevel, logfile, **kwargs)¶ The method the worker calls to execute the task.
Parameters: - request – A
TaskRequest
. - pool – A task pool.
- loglevel – Current loglevel.
- logfile – Name of the currently used logfile.
- consumer – The
Consumer
.
- request – A
-
BaseTask.
expires
= None¶ Default task expiry time.
-
classmethod
BaseTask.
get_consumer
(connection=None, connect_timeout=None)¶ 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()
-
classmethod
BaseTask.
get_logger
(loglevel=None, logfile=None, propagate=False, **kwargs)¶ Get task-aware logger object.
-
classmethod
BaseTask.
get_publisher
(connection=None, exchange=None, connect_timeout=None, exchange_type=None, **options)¶ Get a celery task message publisher.
:rtype
TaskPublisher
: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()
or used as a context:
>>> with self.get_publisher() as publisher: ... # ... do something with publisher
-
BaseTask.
ignore_result
= False¶ If enabled the worker will not store task state and return values for this task. Defaults to the
CELERY_IGNORE_RESULT
setting.
-
BaseTask.
immediate
= False¶ Request immediate delivery.
-
BaseTask.
mandatory
= False¶ Mandatory message routing.
-
BaseTask.
max_retries
= 3¶ Maximum number of retries before giving up. If set to
None
, it will never stop retrying.
-
BaseTask.
name
= None¶ Name of the task.
-
BaseTask.
on_failure
(exc, task_id, args, kwargs, einfo)¶ Error handler.
This is run by the worker when the task fails.
Parameters: - exc – The exception raised by the task.
- task_id – Unique id of the failed task.
- args – Original arguments for the task that failed.
- kwargs – Original keyword arguments for the task that failed.
- einfo –
ExceptionInfo
instance, containing the traceback.
The return value of this handler is ignored.
-
BaseTask.
on_retry
(exc, task_id, args, kwargs, einfo)¶ Retry handler.
This is run by the worker when the task is to be retried.
Parameters: - exc – The exception sent to
retry()
. - task_id – Unique id of the retried task.
- args – Original arguments for the retried task.
- kwargs – Original keyword arguments for the retried task.
- einfo –
ExceptionInfo
instance, containing the traceback.
The return value of this handler is ignored.
- exc – The exception sent to
-
BaseTask.
on_success
(retval, task_id, args, kwargs)¶ Success handler.
Run by the worker if the task executes successfully.
Parameters: - retval – The return value of the task.
- task_id – Unique id of the executed task.
- args – Original arguments for the executed task.
- kwargs – Original keyword arguments for the executed task.
The return value of this handler is ignored.
-
BaseTask.
priority
= None¶ Default message priority. A number between 0 to 9, where 0 is the highest. Note that RabbitMQ does not support priorities.
-
BaseTask.
queue
= None¶ Destination queue. The queue needs to exist in
CELERY_QUEUES
. The routing_key, exchange and exchange_type attributes will be ignored if this is set.
-
BaseTask.
rate_limit
= None¶ 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)
-
BaseTask.
request
= <celery.app.task.Context object>¶ Request context (set when task is applied).
-
classmethod
BaseTask.
retry
(args=None, kwargs=None, exc=None, throw=True, eta=None, countdown=None, max_retries=None, **options)¶ Retry the task.
Parameters: - args – Positional arguments to retry with.
- kwargs – Keyword arguments to retry with.
- exc – Optional exception to raise instead of
MaxRetriesExceededError
when the max restart limit has been exceeded. - countdown – Time in seconds to delay the retry for.
- eta – Explicit time and date to run the retry at
(must be a
datetime
instance). - max_retries – If set, overrides the default retry limit.
- **options – Any extra options to pass on to meth:apply_async.
- throw – If this is
False
, do not raise theRetryTaskError
exception, that tells the worker to mark the task as being retried. Note that this means the task will be marked as failed if the task raises an exception, or successful if it returns.
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.
-
BaseTask.
routing_key
= None¶ Overrides the apps default routing_key for this task.
-
BaseTask.
run
(*args, **kwargs)¶ The body of the task executed by workers.
-
BaseTask.
send_error_emails
= False¶ If enabled an email will be sent to
ADMINS
whenever a task of this type fails.
-
BaseTask.
serializer
= 'pickle'¶ The name of a serializer that are registered with
kombu.serialization.registry
. Default is “pickle”.
-
BaseTask.
soft_time_limit
= None¶ Soft time limit. Defaults to the
CELERY_TASK_SOFT_TIME_LIMIT
setting.
-
BaseTask.
store_errors_even_if_ignored
= False¶ When enabled errors will be stored even if the task is otherwise configured to ignore results.
-
classmethod
BaseTask.
subtask
(*args, **kwargs)¶ Returns
subtask
object for this task, wrapping arguments and execution options for a single task invocation.
-
BaseTask.
time_limit
= None¶ Hard time limit. Defaults to the
CELERY_TASK_TIME_LIMIT
setting.
-
BaseTask.
track_started
= False¶ 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.
-
BaseTask.
type
= 'regular'¶ The type of task (no longer used).
-
classmethod