Celery v0.9.0 (unstable) documentation

This Page

Defining Tasks - celery.task.base

class celery.task.base.AsynchronousMapTask

Task used internally by dmap_async() and TaskSet.map_async().

run(serfunc, args, **kwargs)
The method run by celeryd.
class celery.task.base.ExecuteRemoteTask

Execute an arbitrary function or object.

Note You probably want execute_remote() instead, which this is an internal component of.

The object must be pickleable, so you can’t use lambdas or functions defined in the REPL (that is the python shell, or ipython).

run(ser_callable, fargs, fkwargs, **kwargs)
Parameters:
  • ser_callable – A pickled function or callable object.
  • fargs – Positional arguments to apply to the function.
  • fkwargs – Keyword arguments to apply to the function.
class celery.task.base.PeriodicTask

A periodic task is a task that behaves like a cron job.

run_every
REQUIRED Defines how often the task is run (its interval), it can be either a datetime.timedelta object or an integer specifying the time in seconds.
Raises NotImplementedError:
 if the run_every attribute is not defined.

Example

>>> from celery.task import tasks, PeriodicTask
>>> from datetime import timedelta
>>> class MyPeriodicTask(PeriodicTask):
...     name = "my_periodic_task"
...     run_every = timedelta(seconds=30)
...
...     def run(self, **kwargs):
...         logger = self.get_logger(**kwargs)
...         logger.info("Running MyPeriodicTask")
class celery.task.base.Task

A celery task.

All subclasses of Task must define the run() method, which is the actual method the celery daemon executes.

The run() method can take use of the default keyword arguments, as listed in the run() documentation.

name
Name of the task.
abstract
If ``True`` the task is an abstract base class.
type
The type of task, currently this can be regular, or periodic, however if you want a periodic task, you should subclass PeriodicTask instead.
routing_key
Override the global default routing_key for this task.
exchange
Override the global default exchange for this task.
mandatory
Mandatory message routing. An exception will be raised if the task can’t be routed to a queue.
immediate:
Request immediate delivery. An exception will be raised if the task can’t be routed to a worker immediately.
priority:
The message priority. A number from ``0`` to ``9``, where ``0`` is the
highest. Note that RabbitMQ doesn't support priorities yet.
max_retries
Maximum number of retries before giving up.
default_retry_delay
Default time in seconds before a retry of the task should be executed. Default is a 1 minute delay.
rate_limit
Set the 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)
ignore_result
Don’t store the return value of this task.
disable_error_emails
Disable all error e-mails for this task (only applicable if settings.SEND_CELERY_ERROR_EMAILS is on.)
serializer
The name of a serializer that has been registered with carrot.serialization.registry. Example: "json".
backend
The result store backend used for this task.

The resulting class is callable, which if called will apply the run() method.

exception MaxRetriesExceededError
The tasks max restart limit has been exceeded.
classmethod Task.apply(args=None, kwargs=None, **options)

Execute this task at once, by blocking until the task has finished executing.

Parameters:
  • args – positional arguments passed on to the task.
  • kwargs – keyword arguments passed on to the task.
Return type:

celery.result.EagerResult

See celery.execute.apply().

classmethod Task.apply_async(args=None, kwargs=None, **options)

Delay this task for execution by the celery daemon(s).

Parameters:
  • args – positional arguments passed on to the task.
  • kwargs – keyword arguments passed on to the task.
  • **options – Any keyword arguments to pass on to celery.execute.apply_async().

See celery.execute.apply_async() for more information.

Return type:celery.result.AsyncResult
classmethod Task.delay(*args, **kwargs)

Shortcut to apply_async() but with star arguments, and doesn’t support the extra options.

Parameters:
  • *args – positional arguments passed on to the task.
  • **kwargs – keyword arguments passed on to the task.
Return type:

celery.result.AsyncResult

Task.get_consumer(connect_timeout=4)

Get a celery task message consumer.

Return type:celery.messaging.TaskConsumer.

Please be sure to close the AMQP connection when you’re done with this object. i.e.:

>>> consumer = self.get_consumer()
>>> # do something with consumer
>>> consumer.connection.close()
Task.get_logger(**kwargs)

Get process-aware logger object.

See celery.log.setup_logger().

Task.get_publisher(connect_timeout=4)

Get a celery task message publisher.

Return type:celery.messaging.TaskPublisher.

Please be sure to close the AMQP connection when you’re done with this object, i.e.:

>>> publisher = self.get_publisher()
>>> # do something with publisher
>>> publisher.connection.close()
Task.on_failure(exc, task_id, args, kwargs)

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.

The return value of this handler is ignored.

Task.on_retry(exc, task_id, args, kwargs)

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.

The return value of this handler is ignored.

Task.on_success(retval, task_id, args, kwargs)

Success handler.

This is run by the worker when the task executed 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.

Task.retry(args, kwargs, exc=None, throw=True, **options)

Retry the task.

Parameters:
  • args – Positional arguments to retry with.
  • kwargs – Keyword arguments to retry with.
  • exc – Optional exception to raise instead of MaxRestartsExceededError when the max restart limit has been exceeded.
  • throw – Do not raise the celery.exceptions.RetryTaskError exception, that tells the worker that the task is to be retried.
  • countdown – Time in seconds to delay the retry for.
  • eta – Explicit time and date to run the retry at (must be a datetime.datetime instance).
  • **options – Any extra options to pass on to meth:apply_async. See celery.execute.apply_async().
Raises celery.exceptions.RetryTaskError:
 

To tell the worker that the task has been re-sent for retry. This always happens except if the throw keyword argument has been explicitly set to False.

Example

>>> class TwitterPostStatusTask(Task):
...
...     def run(self, username, password, message, **kwargs):
...         twitter = Twitter(username, password)
...         try:
...             twitter.post_status(message)
...         except twitter.FailWhale, exc:
...             # Retry in 5 minutes.
...             self.retry([username, password, message], kwargs,
...                        countdown=60 * 5, exc=exc)
Task.run(*args, **kwargs)

The body of the task executed by the worker.

The following standard keyword arguments are reserved and is passed by the worker if the function/method supports them:

  • task_id
  • task_name
  • task_retries
  • logfile
  • loglevel

Additional standard keyword arguments may be added in the future. To take these default arguments, the task can either list the ones it wants explicitly or just take an arbitrary list of keyword arguments (**kwargs).

class celery.task.base.TaskSet(task, args)

A task containing several subtasks, making it possible to track how many, or when all of the tasks has been completed.

Parameters:
  • task – The task class or name. Can either be a fully qualified task name, or a task class.
  • args – A list of args, kwargs pairs. e.g. [[args1, kwargs1], [args2, kwargs2], ..., [argsN, kwargsN]]
task_name
The name of the task.
arguments
The arguments, as passed to the task set constructor.
total
Total number of tasks in this task set.

Example

>>> from djangofeeds.tasks import RefreshFeedTask
>>> taskset = TaskSet(RefreshFeedTask, args=[
...                 ([], {"feed_url": "http://cnn.com/rss"}),
...                 ([], {"feed_url": "http://bbc.com/rss"}),
...                 ([], {"feed_url": "http://xkcd.com/rss"})
... ])
>>> taskset_result = taskset.run()
>>> list_of_return_values = taskset_result.join()
classmethod map(func, args, timeout=None)
Distribute processing of the arguments and collect the results.
classmethod map_async(func, args, timeout=None)

Distribute processing of the arguments and collect the results asynchronously.

Returns:celery.result.AsyncResult instance.
classmethod remote_execute(func, args)
Apply args to function by distributing the args to the celery server(s).
run(connect_timeout=4)

Run all tasks in the taskset.

Returns:A celery.result.TaskSetResult instance.

Example

>>> ts = TaskSet(RefreshFeedTask, args=[
...         (["http://foo.com/rss"], {}),
...         (["http://bar.com/rss"], {}),
... ])
>>> result = ts.run()
>>> result.taskset_id
"d2c9b261-8eff-4bfb-8459-1e1b72063514"
>>> result.subtask_ids
["b4996460-d959-49c8-aeb9-39c530dcde25",
"598d2d18-ab86-45ca-8b4f-0779f5d6a3cb"]
>>> result.waiting()
True
>>> time.sleep(10)
>>> result.ready()
True
>>> result.successful()
True
>>> result.failed()
False
>>> result.join()
[True, True]
class celery.task.base.TaskType

Metaclass for tasks.

Automatically registers the task in the task registry, except if the abstract attribute is set.

If no name attribute is provided, the name is automatically set to the name of the module it was defined in, and the class name.