Tasks

Tasks are the building blocks of Celery applications.

A task is a class that can be created out of any callable. It performs dual roles in that it defines both what happens when a task is called (sends a message), and what happens when a worker receives that message.

Every task class has a unique name, and this name is referenced in messages so that the worker can find the right function to execute.

A task message does not disappear until the message has been acknowledged by a worker. A worker can reserve many messages in advance and even if the worker is killed – caused by power failure or otherwise – the message will be redelivered to another worker.

Ideally task functions should be idempotent, which means that the function will not cause unintented effects even if called multiple times with the same arguments. Since the worker cannot detect if your tasks are idempotent, the default behavior is to acknowledge the message in advance, before it’s executed, so that a task that has already been started is never executed again..

If your task is idempotent you can set the acks_late option to have the worker acknowledge the message after the task returns instead. See also the FAQ entry Should I use retry or acks_late?.

In this chapter you will learn all about defining tasks, and this is the table of contents:

Basics

You can easily create a task from any callable by using the task() decorator:

  1. from .models import User
  2. @app.task
  3. def create_user(username, password):
  4. User.objects.create(username=username, password=password)

There are also many options that can be set for the task, these can be specified as arguments to the decorator:

  1. @app.task(serializer='json')
  2. def create_user(username, password):
  3. User.objects.create(username=username, password=password)

How do I import the task decorator? And what is “app”?

The task decorator is available on your Celery application instance, if you don’t know what that is then please read First Steps with Celery.

If you’re using Django or are still using the “old” module based celery API, then you can import the task decorator like this:

  1. from celery import task
  2. @task
  3. def add(x, y):
  4. return x + y

Multiple decorators

When using multiple decorators in combination with the task decorator you must make sure that the task decorator is applied last (which in Python oddly means that it must be the first in the list):

  1. @app.task
  2. @decorator2
  3. @decorator1
  4. def add(x, y):
  5. return x + y

Names

Every task must have a unique name, and a new name will be generated out of the function name if a custom name is not provided.

For example:

  1. >>> @app.task(name='sum-of-two-numbers')
  2. >>> def add(x, y):
  3. ... return x + y
  4. >>> add.name
  5. 'sum-of-two-numbers'

A best practice is to use the module name as a namespace, this way names won’t collide if there’s already a task with that name defined in another module.

  1. >>> @app.task(name='tasks.add')
  2. >>> def add(x, y):
  3. ... return x + y

You can tell the name of the task by investigating its name attribute:

  1. >>> add.name
  2. 'tasks.add'

Which is exactly the name that would have been generated anyway, if the module name is “tasks.py”:

tasks.py:

  1. @app.task
  2. def add(x, y):
  3. return x + y
  4. >>> from tasks import add
  5. >>> add.name
  6. 'tasks.add'

Automatic naming and relative imports

Relative imports and automatic name generation does not go well together, so if you’re using relative imports you should set the name explicitly.

For example if the client imports the module “myapp.tasks” as ”.tasks”, and the worker imports the module as “myapp.tasks”, the generated names won’t match and an NotRegistered error will be raised by the worker.

This is also the case when using Django and using project.myapp-style naming in INSTALLED_APPS:

  1. INSTALLED_APPS = ['project.myapp']

If you install the app under the name project.myapp then the tasks module will be imported as project.myapp.tasks, so you must make sure you always import the tasks using the same name:

  1. >>> from project.myapp.tasks import mytask # << GOOD
  2. >>> from myapp.tasks import mytask # << BAD!!!

The second example will cause the task to be named differently since the worker and the client imports the modules under different names:

  1. >>> from project.myapp.tasks import mytask
  2. >>> mytask.name
  3. 'project.myapp.tasks.mytask'
  4. >>> from myapp.tasks import mytask
  5. >>> mytask.name
  6. 'myapp.tasks.mytask'

So for this reason you must be consistent in how you import modules, which is also a Python best practice.

Similarly, you should not use old-style relative imports:

  1. from module import foo # BAD!
  2. from proj.module import foo # GOOD!

New-style relative imports are fine and can be used:

  1. from .module import foo # GOOD!

If you want to use Celery with a project already using these patterns extensively and you don’t have the time to refactor the existing code then you can consider specifying the names explicitly instead of relying on the automatic naming:

  1. @task(name='proj.tasks.add')
  2. def add(x, y):
  3. return x + y

Context

request contains information and state related to the executing task.

The request defines the following attributes:

id:The unique id of the executing task.
group:The unique id a group, if this task is a member.
chord:The unique id of the chord this task belongs to (if the task is part of the header).
args:Positional arguments.
kwargs:Keyword arguments.
retries:How many times the current task has been retried. An integer starting at 0.
is_eager:Set to True if the task is executed locally in the client, and not by a worker.
eta:The original ETA of the task (if any). This is in UTC time (depending on the CELERY_ENABLE_UTC setting).
expires:The original expiry time of the task (if any). This is in UTC time (depending on the CELERY_ENABLE_UTC setting).
logfile:The file the worker logs to. See Logging.
loglevel:The current log level used.
hostname:Hostname of the worker instance executing the task.
delivery_info:Additional message delivery information. This is a mapping containing the exchange and routing key used to deliver this task. Used by e.g. retry() to resend the task to the same destination queue. Availability of keys in this dict depends on the message broker used.
called_directly:
 This flag is set to true if the task was not executed by the worker.
callbacks:A list of subtasks to be called if this task returns successfully.
errback:A list of subtasks to be called if this task fails.
utc:Set to true the caller has utc enabled (CELERY_ENABLE_UTC).

3.1 新版功能.

headers:Mapping of message headers (may be None).
reply_to:Where to send reply to (queue name).
correlation_id:Usually the same as the task id, often used in amqp to keep track of what a reply is for.

An example task accessing information in the context is:

  1. @app.task(bind=True)
  2. def dump_context(self, x, y):
  3. print('Executing task id {0.id}, args: {0.args!r} kwargs: {0.kwargs!r}'.format(
  4. self.request))

The bind argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance.

Logging

The worker will automatically set up logging for you, or you can configure logging manually.

A special logger is available named “celery.task”, you can inherit from this logger to automatically get the task name and unique id as part of the logs.

The best practice is to create a common logger for all of your tasks at the top of your module:

  1. from celery.utils.log import get_task_logger
  2. logger = get_task_logger(__name__)
  3. @app.task
  4. def add(x, y):
  5. logger.info('Adding {0} + {1}'.format(x, y))
  6. return x + y

Celery uses the standard Python logger library, for which documentation can be found in the logging module.

You can also use print(), as anything written to standard out/-err will be redirected to logging system (you can disable this, see CELERY_REDIRECT_STDOUTS).

Retrying

retry() can be used to re-execute the task, for example in the event of recoverable errors.

When you call retry it will send a new message, using the same task-id, and it will take care to make sure the message is delivered to the same queue as the originating task.

When a task is retried this is also recorded as a task state, so that you can track the progress of the task using the result instance (see States).

Here’s an example using retry:

  1. @app.task(bind=True)
  2. def send_twitter_status(self, oauth, tweet):
  3. try:
  4. twitter = Twitter(oauth)
  5. twitter.update_status(tweet)
  6. except (Twitter.FailWhaleError, Twitter.LoginError) as exc:
  7. raise self.retry(exc=exc)

注解

The retry() call will raise an exception so any code after the retry will not be reached. This is the Retry exception, it is not handled as an error but rather as a semi-predicate to signify to the worker that the task is to be retried, so that it can store the correct state when a result backend is enabled.

This is normal operation and always happens unless the throw argument to retry is set to False.

The bind argument to the task decorator will give access to self (the task type instance).

The exc method is used to pass exception information that is used in logs, and when storing task results. Both the exception and the traceback will be available in the task state (if a result backend is enabled).

If the task has a max_retries value the current exception will be re-raised if the max number of retries has been exceeded, but this will not happen if:

  • An exc argument was not given.

    In this case the MaxRetriesExceeded exception will be raised.

  • There is no current exception

    If there’s no original exception to re-raise the exc argument will be used instead, so:

    1. self.retry(exc=Twitter.LoginError())

    will raise the exc argument given.

Using a custom retry delay

When a task is to be retried, it can wait for a given amount of time before doing so, and the default delay is defined by the default_retry_delay attribute. By default this is set to 3 minutes. Note that the unit for setting the delay is in seconds (int or float).

You can also provide the countdown argument to retry() to override this default.

  1. @app.task(bind=True, default_retry_delay=30 * 60) # retry in 30 minutes.
  2. def add(self, x, y):
  3. try:
  4. except Exception as exc:
  5. raise self.retry(exc=exc, countdown=60) # override the default and
  6. # retry in 1 minute

List of Options

The task decorator can take a number of options that change the way the task behaves, for example you can set the rate limit for a task using the rate_limit option.

Any keyword argument passed to the task decorator will actually be set as an attribute of the resulting task class, and this is a list of the built-in attributes.

General

Task.name

The name the task is registered as.

You can set this name manually, or a name will be automatically generated using the module and class name. See Names.

Task.request

If the task is being executed this will contain information about the current request. Thread local storage is used.

See Context.

Task.abstract

Abstract classes are not registered, but are used as the base class for new task types.

Task.max_retries

The maximum number of attempted retries before giving up. If the number of retries exceeds this value a MaxRetriesExceeded exception will be raised. NOTE: You have to call retry() manually, as it will not automatically retry on exception..

The default value is 3. A value of None will disable the retry limit and the task will retry forever until it succeeds.

Task.throws

Optional list of expected error classes that should not be regarded as an actual error.

Errors in this list will be reported as a failure to the result backend, but the worker will not log the event as an error, and no traceback will be included.

Example:

  1. @task(throws=(KeyError, HttpNotFound)):
  2. def get_foo():
  3. something()

Error types:

  • Expected errors (in Task.throws)

    Logged with severity INFO, traceback excluded.

  • Unexpected errors

    Logged with severity ERROR, with traceback included.

Task.default_retry_delay

Default time in seconds before a retry of the task should be executed. Can be either int or float. Default is a 3 minute delay.

Task.rate_limit

Set the rate limit for this task type which limits the number of tasks that can be run in a given time frame. Tasks will still complete when a rate limit is in effect, but it may take some time before it’s allowed to start.

If this is None no rate limit is in effect. If it is an integer or float, it is interpreted as “tasks per second”.

The rate limits can be specified in seconds, minutes or hours by appending “/s”, “/m” or “/h” to the value. Example: “100/m” (hundred tasks a minute). Default is the CELERY_DEFAULT_RATE_LIMIT setting, which if not specified means rate limiting for tasks is disabled by default.

Task.time_limit

The hard time limit for this task. If not set then the workers default will be used.

Task.soft_time_limit

The soft time limit for this task. If not set then the workers default will be used.

Task.ignore_result

Don’t store task state. Note that this means you can’t use AsyncResult to check if the task is ready, or get its return value.

Task.store_errors_even_if_ignored

If True, errors will be stored even if the task is configured to ignore results.

Task.send_error_emails

Send an email whenever a task of this type fails. Defaults to the CELERY_SEND_TASK_ERROR_EMAILS setting. See Error E-Mails for more information.

Task.ErrorMail

If the sending of error emails is enabled for this task, then this is the class defining the logic to send error mails.

Task.serializer

A string identifying the default serialization method to use. Defaults to the CELERY_TASK_SERIALIZER setting. Can be pickle json, yaml, or any custom serialization methods that have been registered with kombu.serialization.registry.

Please see Serializers for more information.

Task.compression

A string identifying the default compression scheme to use.

Defaults to the CELERY_MESSAGE_COMPRESSION setting. Can be gzip, or bzip2, or any custom compression schemes that have been registered with the kombu.compression registry.

Please see Compression for more information.

Task.backend

The result store backend to use for this task. Defaults to the CELERY_RESULT_BACKEND setting.

Task.acks_late

If set to True messages for this task will be acknowledged after the task has been executed, not just before, which is the default behavior.

Note that this means the task may be executed twice if the worker crashes in the middle of execution, which may be acceptable for some applications.

The global default can be overridden by the CELERY_ACKS_LATE setting.

Task.track_started

If True the task will report its status as “started” when the task is executed by a worker. The default value is False 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 host name and process id of the worker executing the task will be available in the state metadata (e.g. result.info[‘pid’])

The global default can be overridden by the CELERY_TRACK_STARTED setting.

参见

The API reference for Task.

States

Celery can keep track of the tasks current state. The state also contains the result of a successful task, or the exception and traceback information of a failed task.

There are several result backends to choose from, and they all have different strengths and weaknesses (see Result Backends).

During its lifetime a task will transition through several possible states, and each state may have arbitrary metadata attached to it. When a task moves into a new state the previous state is forgotten about, but some transitions can be deducted, (e.g. a task now in the FAILED state, is implied to have been in the STARTED state at some point).

There are also sets of states, like the set of FAILURE_STATES, and the set of READY_STATES.

The client uses the membership of these sets to decide whether the exception should be re-raised (PROPAGATE_STATES), or whether the state can be cached (it can if the task is ready).

You can also define Custom states.

Result Backends

If you want to keep track of tasks or need the return values, then Celery must store or send the states somewhere so that they can be retrieved later. There are several built-in result backends to choose from: SQLAlchemy/Django ORM, Memcached, RabbitMQ (amqp), MongoDB, and Redis – or you can define your own.

No backend works well for every use case. You should read about the strengths and weaknesses of each backend, and choose the most appropriate for your needs.

参见

Task result backend settings

RabbitMQ Result Backend

The RabbitMQ result backend (amqp) is special as it does not actually store the states, but rather sends them as messages. This is an important difference as it means that a result can only be retrieved once; If you have two processes waiting for the same result, one of the processes will never receive the result!

Even with that limitation, it is an excellent choice if you need to receive state changes in real-time. Using messaging means the client does not have to poll for new states.

There are several other pitfalls you should be aware of when using the RabbitMQ result backend:

  • Every new task creates a new queue on the server, with thousands of tasks the broker may be overloaded with queues and this will affect performance in negative ways. If you’re using RabbitMQ then each queue will be a separate Erlang process, so if you’re planning to keep many results simultaneously you may have to increase the Erlang process limit, and the maximum number of file descriptors your OS allows.
  • Old results will be cleaned automatically, based on the CELERY_TASK_RESULT_EXPIRES setting. By default this is set to expire after 1 day: if you have a very busy cluster you should lower this value.

For a list of options supported by the RabbitMQ result backend, please see AMQP backend settings.

Database Result Backend

Keeping state in the database can be convenient for many, especially for web applications with a database already in place, but it also comes with limitations.

  • Polling the database for new states is expensive, and so you should increase the polling intervals of operations such as result.get().

  • Some databases use a default transaction isolation level that is not suitable for polling tables for changes.

    In MySQL the default transaction isolation level is REPEATABLE-READ, which means the transaction will not see changes by other transactions until the transaction is committed. It is recommended that you change to the READ-COMMITTED isolation level.

Built-in States

PENDING

Task is waiting for execution or unknown. Any task id that is not known is implied to be in the pending state.

STARTED

Task has been started. Not reported by default, to enable please see celery.Task.track_started.

metadata:pid and hostname of the worker process executing the task.

SUCCESS

Task has been successfully executed.

metadata:result contains the return value of the task.
propagates:Yes
ready:Yes

FAILURE

Task execution resulted in failure.

metadata:result contains the exception occurred, and traceback contains the backtrace of the stack at the point when the exception was raised.
propagates:Yes

RETRY

Task is being retried.

metadata:result contains the exception that caused the retry, and traceback contains the backtrace of the stack at the point when the exceptions was raised.
propagates:No

REVOKED

Task has been revoked.

propagates:Yes

Custom states

You can easily define your own states, all you need is a unique name. The name of the state is usually an uppercase string. As an example you could have a look at abortable tasks which defines its own custom ABORTED state.

Use update_state() to update a task’s state:

  1. @app.task(bind=True)
  2. def upload_files(self, filenames):
  3. for i, file in enumerate(filenames):
  4. self.update_state(state='PROGRESS',
  5. meta={'current': i, 'total': len(filenames)})

Here I created the state “PROGRESS”, which tells any application aware of this state that the task is currently in progress, and also where it is in the process by having current and total counts as part of the state metadata. This can then be used to create e.g. progress bars.

Creating pickleable exceptions

A rarely known Python fact is that exceptions must conform to some simple rules to support being serialized by the pickle module.

Tasks that raise exceptions that are not pickleable will not work properly when Pickle is used as the serializer.

To make sure that your exceptions are pickleable the exception MUST provide the original arguments it was instantiated with in its .args attribute. The simplest way to ensure this is to have the exception call Exception.__init__.

Let’s look at some examples that work, and one that doesn’t:

  1. # OK:
  2. class HttpError(Exception):
  3. pass
  4. # BAD:
  5. class HttpError(Exception):
  6. def __init__(self, status_code):
  7. self.status_code = status_code
  8. # OK:
  9. class HttpError(Exception):
  10. def __init__(self, status_code):
  11. self.status_code = status_code
  12. Exception.__init__(self, status_code) # <-- REQUIRED

So the rule is: For any exception that supports custom arguments *args, Exception.__init__(self, *args) must be used.

There is no special support for keyword arguments, so if you want to preserve keyword arguments when the exception is unpickled you have to pass them as regular args:

  1. class HttpError(Exception):
  2. def __init__(self, status_code, headers=None, body=None):
  3. self.status_code = status_code
  4. self.headers = headers
  5. self.body = body
  6. super(HttpError, self).__init__(status_code, headers, body)

Semipredicates

The worker wraps the task in a tracing function which records the final state of the task. There are a number of exceptions that can be used to signal this function to change how it treats the return of the task.

Ignore

The task may raise Ignore to force the worker to ignore the task. This means that no state will be recorded for the task, but the message is still acknowledged (removed from queue).

This is can be used if you want to implement custom revoke-like functionality, or manually store the result of a task.

Example keeping revoked tasks in a Redis set:

  1. from celery.exceptions import Ignore
  2. @app.task(bind=True)
  3. def some_task(self):
  4. if redis.ismember('tasks.revoked', self.request.id):
  5. raise Ignore()

Example that stores results manually:

  1. from celery import states
  2. from celery.exceptions import Ignore
  3. @app.task(bind=True)
  4. def get_tweets(self, user):
  5. timeline = twitter.get_timeline(user)
  6. self.update_state(sate=states.SUCCESS, meta=timeline)
  7. raise Ignore()

Reject

The task may raise Reject to reject the task message using AMQPs basic_reject method. This will not have any effect unless Task.acks_late is enabled.

Rejecting a message has the same effect as acking it, but some brokers may implement additional functionality that can be used. For example RabbitMQ supports the concept of Dead Letter Exchanges where a queue can be configured to use a dead letter exchange that rejected messages are redelivered to.

Reject can also be used to requeue messages, but please be very careful when using this as it can easily result in an infinite message loop.

Example using reject when a task causes an out of memory condition:

  1. import errno
  2. from celery.exceptions import Reject
  3. @app.task(bind=True, acks_late=True)
  4. def render_scene(self, path):
  5. file = get_file(path)
  6. try:
  7. renderer.render_scene(file)
  8. # if the file is too big to fit in memory
  9. # we reject it so that it's redelivered to the dead letter exchange
  10. # and we can manually inspect the situation.
  11. except MemoryError as exc:
  12. raise Reject(exc, requeue=False)
  13. except OSError as exc:
  14. if exc.errno == errno.ENOMEM:
  15. raise Reject(exc, requeue=False)
  16. # For any other error we retry after 10 seconds.
  17. except Exception as exc:
  18. raise self.retry(exc, countdown=10)

Example requeuing the message:

  1. import errno
  2. from celery.exceptions import Reject
  3. @app.task(bind=True, acks_late=True)
  4. def requeues(self):
  5. if not self.request.delivery_info['redelivered']:
  6. raise Requeue('no reason', requeue=True)
  7. print('received two times')

Consult your broker documentation for more details about the basic_reject method.

Retry

The Retry exception is raised by the Task.retry method to tell the worker that the task is being retried.

Custom task classes

All tasks inherit from the celery.Task class. The run() method becomes the task body.

As an example, the following code,

  1. @app.task
  2. def add(x, y):
  3. return x + y

will do roughly this behind the scenes:

  1. class _AddTask(app.Task):
  2. def run(self, x, y):
  3. return x + y
  4. add = app.tasks[_AddTask.name]

Instantiation

A task is not instantiated for every request, but is registered in the task registry as a global instance.

This means that the __init__ constructor will only be called once per process, and that the task class is semantically closer to an Actor.

If you have a task,

  1. from celery import Task
  2. class NaiveAuthenticateServer(Task):
  3. def __init__(self):
  4. self.users = {'george': 'password'}
  5. def run(self, username, password):
  6. try:
  7. return self.users[username] == password
  8. except KeyError:
  9. return False

And you route every request to the same process, then it will keep state between requests.

This can also be useful to cache resources, e.g. a base Task class that caches a database connection:

  1. from celery import Task
  2. class DatabaseTask(Task):
  3. abstract = True
  4. _db = None
  5. @property
  6. def db(self):
  7. if self._db is None:
  8. self._db = Database.connect()
  9. return self._db

that can be added to tasks like this:

  1. @app.task(base=DatabaseTask)
  2. def process_rows():
  3. for row in process_rows.db.table.all():

The db attribute of the process_rows task will then always stay the same in each process.

Abstract classes

Abstract classes are not registered, but are used as the base class for new task types.

  1. from celery import Task
  2. class DebugTask(Task):
  3. abstract = True
  4. def after_return(self, *args, **kwargs):
  5. print('Task returned: {0!r}'.format(self.request)
  6. @app.task(base=DebugTask)
  7. def add(x, y):
  8. return x + y

Handlers

after_return(self, status, retval, task_id, args, kwargs, einfo)

Handler called after the task returns.

参数:
  • status – Current task state.
  • retval – Task return value/exception.
  • task_id – Unique id of the task.
  • args – Original arguments for the task that returned.
  • kwargs – Original keyword arguments for the task that returned.
  • einfoExceptionInfo instance, containing the traceback (if any).

The return value of this handler is ignored.

on_failure(self, exc, task_id, args, kwargs, einfo)

This is run by the worker when the task fails.

参数:
  • 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.
  • einfoExceptionInfo instance, containing the traceback.

The return value of this handler is ignored.

on_retry(self, exc, task_id, args, kwargs, einfo)

This is run by the worker when the task is to be retried.

参数:
  • 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.
  • einfoExceptionInfo instance, containing the traceback.

The return value of this handler is ignored.

on_success(self, retval, task_id, args, kwargs)

Run by the worker if the task executes successfully.

参数:
  • 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.

on_retry

How it works

Here comes the technical details, this part isn’t something you need to know, but you may be interested.

All defined tasks are listed in a registry. The registry contains a list of task names and their task classes. You can investigate this registry yourself:

  1. >>> from celery import current_app
  2. >>> current_app.tasks
  3. {'celery.chord_unlock':
  4. <@task: celery.chord_unlock>,
  5. 'celery.backend_cleanup':
  6. <@task: celery.backend_cleanup>,
  7. 'celery.chord':
  8. <@task: celery.chord>}

This is the list of tasks built-in to celery. Note that tasks will only be registered when the module they are defined in is imported.

The default loader imports any modules listed in the CELERY_IMPORTS setting.

The entity responsible for registering your task in the registry is the metaclass: TaskType.

If you want to register your task manually you can mark the task as abstract:

  1. class MyTask(Task):
  2. abstract = True

This way the task won’t be registered, but any task inheriting from it will be.

When tasks are sent, no actual function code is sent with it, just the name of the task to execute. When the worker then receives the message it can look up the name in its task registry to find the execution code.

This means that your workers should always be updated with the same software as the client. This is a drawback, but the alternative is a technical challenge that has yet to be solved.

Tips and Best Practices

Ignore results you don’t want

If you don’t care about the results of a task, be sure to set the ignore_result option, as storing results wastes time and resources.

  1. @app.task(ignore_result=True)
  2. def mytask(…):
  3. something()

Results can even be disabled globally using the CELERY_IGNORE_RESULT setting.

Disable rate limits if they’re not used

Disabling rate limits altogether is recommended if you don’t have any tasks using them. This is because the rate limit subsystem introduces quite a lot of complexity.

Set the CELERY_DISABLE_RATE_LIMITS setting to globally disable rate limits:

  1. CELERY_DISABLE_RATE_LIMITS = True

You find additional optimization tips in the Optimizing Guide.

Avoid launching synchronous subtasks

Having a task wait for the result of another task is really inefficient, and may even cause a deadlock if the worker pool is exhausted.

Make your design asynchronous instead, for example by using callbacks.

Bad:

  1. @app.task
  2. def update_page_info(url):
  3. page = fetch_page.delay(url).get()
  4. info = parse_page.delay(url, page).get()
  5. store_page_info.delay(url, info)
  6. @app.task
  7. def fetch_page(url):
  8. return myhttplib.get(url)
  9. @app.task
  10. def parse_page(url, page):
  11. return myparser.parse_document(page)
  12. @app.task
  13. def store_page_info(url, info):
  14. return PageInfo.objects.create(url, info)

Good:

  1. def update_page_info(url):
  2. # fetch_page -> parse_page -> store_page
  3. chain = fetch_page.s() | parse_page.s() | store_page_info.s(url)
  4. chain()
  5. @app.task()
  6. def fetch_page(url):
  7. return myhttplib.get(url)
  8. @app.task()
  9. def parse_page(page):
  10. return myparser.parse_document(page)
  11. @app.task(ignore_result=True)
  12. def store_page_info(info, url):
  13. PageInfo.objects.create(url=url, info=info)

Here I instead created a chain of tasks by linking together different subtask()‘s. You can read about chains and other powerful constructs at Canvas: Designing Workflows.

Performance and Strategies

Granularity

The task granularity is the amount of computation needed by each subtask. In general it is better to split the problem up into many small tasks, than have a few long running tasks.

With smaller tasks you can process more tasks in parallel and the tasks won’t run long enough to block the worker from processing other waiting tasks.

However, executing a task does have overhead. A message needs to be sent, data may not be local, etc. So if the tasks are too fine-grained the additional overhead may not be worth it in the end.

参见

The book Art of Concurrency has a section dedicated to the topic of task granularity [AOC1].

[AOC1]Breshears, Clay. Section 2.2.1, “The Art of Concurrency”. O’Reilly Media, Inc. May 15, 2009. ISBN-13 978-0-596-52153-0.

Data locality

The worker processing the task should be as close to the data as possible. The best would be to have a copy in memory, the worst would be a full transfer from another continent.

If the data is far away, you could try to run another worker at location, or if that’s not possible - cache often used data, or preload data you know is going to be used.

The easiest way to share data between workers is to use a distributed cache system, like memcached.

参见

The paper Distributed Computing Economics by Jim Gray is an excellent introduction to the topic of data locality.

State

Since celery is a distributed system, you can’t know in which process, or on what machine the task will be executed. You can’t even know if the task will run in a timely manner.

The ancient async sayings tells us that “asserting the world is the responsibility of the task”. What this means is that the world view may have changed since the task was requested, so the task is responsible for making sure the world is how it should be; If you have a task that re-indexes a search engine, and the search engine should only be re-indexed at maximum every 5 minutes, then it must be the tasks responsibility to assert that, not the callers.

Another gotcha is Django model objects. They shouldn’t be passed on as arguments to tasks. It’s almost always better to re-fetch the object from the database when the task is running instead, as using old data may lead to race conditions.

Imagine the following scenario where you have an article and a task that automatically expands some abbreviations in it:

  1. class Article(models.Model):
  2. title = models.CharField()
  3. body = models.TextField()
  4. @app.task
  5. def expand_abbreviations(article):
  6. article.body.replace('MyCorp', 'My Corporation')
  7. article.save()

First, an author creates an article and saves it, then the author clicks on a button that initiates the abbreviation task:

  1. >>> article = Article.objects.get(id=102)
  2. >>> expand_abbreviations.delay(article)

Now, the queue is very busy, so the task won’t be run for another 2 minutes. In the meantime another author makes changes to the article, so when the task is finally run, the body of the article is reverted to the old version because the task had the old body in its argument.

Fixing the race condition is easy, just use the article id instead, and re-fetch the article in the task body:

  1. @app.task
  2. def expand_abbreviations(article_id):
  3. article = Article.objects.get(id=article_id)
  4. article.body.replace('MyCorp', 'My Corporation')
  5. article.save()
  6. >>> expand_abbreviations(article_id)

There might even be performance benefits to this approach, as sending large messages may be expensive.

Database transactions

Let’s have a look at another example:

  1. from django.db import transaction
  2. @transaction.commit_on_success
  3. def create_article(request):
  4. article = Article.objects.create(…)
  5. expand_abbreviations.delay(article.pk)

This is a Django view creating an article object in the database, then passing the primary key to a task. It uses the commit_on_success decorator, which will commit the transaction when the view returns, or roll back if the view raises an exception.

There is a race condition if the task starts executing before the transaction has been committed; The database object does not exist yet!

The solution is to always commit transactions before sending tasks depending on state from the current transaction:

  1. @transaction.commit_manually
  2. def create_article(request):
  3. try:
  4. article = Article.objects.create(…)
  5. except:
  6. transaction.rollback()
  7. raise
  8. else:
  9. transaction.commit()
  10. expand_abbreviations.delay(article.pk)

Example

Let’s take a real wold example; A blog where comments posted needs to be filtered for spam. When the comment is created, the spam filter runs in the background, so the user doesn’t have to wait for it to finish.

I have a Django blog application allowing comments on blog posts. I’ll describe parts of the models/views and tasks for this application.

blog/models.py

The comment model looks like this:

  1. from django.db import models
  2. from django.utils.translation import ugettext_lazy as _
  3. class Comment(models.Model):
  4. name = models.CharField(_('name'), max_length=64)
  5. email_address = models.EmailField(_('email address'))
  6. homepage = models.URLField(_('home page'),
  7. blank=True, verify_exists=False)
  8. comment = models.TextField(_('comment'))
  9. pub_date = models.DateTimeField(_('Published date'),
  10. editable=False, auto_add_now=True)
  11. is_spam = models.BooleanField(_('spam?'),
  12. default=False, editable=False)
  13. class Meta:
  14. verbose_name = _('comment')
  15. verbose_name_plural = _('comments')

In the view where the comment is posted, I first write the comment to the database, then I launch the spam filter task in the background.

blog/views.py

  1. from django import forms
  2. from django.http import HttpResponseRedirect
  3. from django.template.context import RequestContext
  4. from django.shortcuts import get_object_or_404, render_to_response
  5. from blog import tasks
  6. from blog.models import Comment
  7. class CommentForm(forms.ModelForm):
  8. class Meta:
  9. model = Comment
  10. def add_comment(request, slug, template_name='comments/create.html'):
  11. post = get_object_or_404(Entry, slug=slug)
  12. remote_addr = request.META.get('REMOTE_ADDR')
  13. if request.method == 'post':
  14. form = CommentForm(request.POST, request.FILES)
  15. if form.is_valid():
  16. comment = form.save()
  17. # Check spam asynchronously.
  18. tasks.spam_filter.delay(comment_id=comment.id,
  19. remote_addr=remote_addr)
  20. return HttpResponseRedirect(post.get_absolute_url())
  21. else:
  22. form = CommentForm()
  23. context = RequestContext(request, {'form': form})
  24. return render_to_response(template_name, context_instance=context)

To filter spam in comments I use Akismet, the service used to filter spam in comments posted to the free weblog platform Wordpress. Akismet is free for personal use, but for commercial use you need to pay. You have to sign up to their service to get an API key.

To make API calls to Akismet I use the akismet.py library written by Michael Foord.

blog/tasks.py

  1. from celery import Celery
  2. from akismet import Akismet
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.contrib.sites.models import Site
  5. from blog.models import Comment
  6. app = Celery(broker='amqp://')
  7. @app.task
  8. def spam_filter(comment_id, remote_addr=None):
  9. logger = spam_filter.get_logger()
  10. logger.info('Running spam filter for comment %s', comment_id)
  11. comment = Comment.objects.get(pk=comment_id)
  12. current_domain = Site.objects.get_current().domain
  13. akismet = Akismet(settings.AKISMET_KEY, 'http://{0}'.format(domain))
  14. if not akismet.verify_key():
  15. raise ImproperlyConfigured('Invalid AKISMET_KEY')
  16. is_spam = akismet.comment_check(user_ip=remote_addr,
  17. comment_content=comment.comment,
  18. comment_author=comment.name,
  19. comment_author_email=comment.email_address)
  20. if is_spam:
  21. comment.is_spam = True
  22. comment.save()
  23. return is_spam