Skip to content
Myr

Merge pull request #2055 from geoffreybauduin/celery-documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Updated Celery pattern

authored by David Lord · committed by GitHub · parents ff361d3, d5a88bf · Browse files · 1 file+44 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+44 added−36 removed

  • other:+44 added−36 removed

1 file+44 added−36 removed

modified:docs/patterns/celery.rst+44 added-36 removed
1Celery Based Background Tasks2=============================1Celery Background Tasks2=======================34Celery is a task queue for Python with batteries included. It used to5have a Flask integration but it became unnecessary after some6restructuring of the internals of Celery with Version 3. This guide fills7in the blanks in how to properly use Celery with Flask but assumes that8you generally already read the `First Steps with Celery9<http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html>`_10guide in the official Celery documentation.4If your application has a long running task, such as processing some uploaded5data or sending email, you don't want to wait for it to finish during a6request. Instead, use a task queue to send the necessary data to another7process that will run the task in the background while the request returns8immediately.912Installing Celery13-----------------10Celery is a powerful task queue that can be used for simple background tasks11as well as complex multi-stage programs and schedules. This guide will show you12how to configure Celery using Flask, but assumes you've already read the13`First Steps with Celery <http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html>`_14guide in the Celery documentation.1515Celery is on the Python Package Index (PyPI), so it can be installed with16standard Python tools like :command:`pip` or :command:`easy_install`::16Install17-------1819Celery is a separate Python package. Install it from PyPI using pip::2021 $ pip install celery2220Configuring Celery21------------------23Configure24---------2526The first thing you need is a Celery instance, this is called the celery27application. It serves the same purpose as the :class:`~flask.Flask`28object in Flask, just for Celery. Since this instance is used as the
9 unmodified lines
3839 from celery import Celery4041 def make_celery(app):42 celery = Celery(43 app.import_name,39 celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],40 broker=app.config['CELERY_BROKER_URL'])44 backend=app.config['CELERY_RESULT_BACKEND'],45 broker=app.config['CELERY_BROKER_URL']46 )47 celery.conf.update(app.config)42 TaskBase = celery.Task43 class ContextTask(TaskBase):44 abstract = True4849 class ContextTask(celery.Task):50 def __call__(self, *args, **kwargs):51 with app.app_context():47 return TaskBase.__call__(self, *args, **kwargs)52 return self.run(*args, **kwargs)5354 celery.Task = ContextTask55 return celery5657The function creates a new Celery object, configures it with the broker58from the application config, updates the rest of the Celery config from59the Flask config and then creates a subclass of the task that wraps the60task execution in an application context.6156Minimal Example62An example task63---------------6459With what we have above this is the minimal example of using Celery with60Flask::65Let's write a task that adds two numbers together and returns the result. We66configure Celery's broker and backend to use Redis, create a ``celery``67application using the factor from above, and then use it to define the task. ::6869 from flask import Flask7071 flask_app = Flask(__name__)
2 unmodified lines
74 CELERY_RESULT_BACKEND='redis://localhost:6379'75 )76 celery = make_celery(flask_app)777178 @celery.task()79 def add_together(a, b):80 return a + b8176This task can now be called in the background:82This task can now be called in the background::8378>>> result = add_together.delay(23, 42)79>>> result.wait()806584 result = add_together.delay(23, 42)85 result.wait() # 658682Running the Celery Worker83-------------------------87Run a worker88------------8985Now if you jumped in and already executed the above code you will be86disappointed to learn that your ``.wait()`` will never actually return.87That's because you also need to run celery. You can do that by running88celery as a worker::90If you jumped in and already executed the above code you will be91disappointed to learn that ``.wait()`` will never actually return.92That's because you also need to run a Celery worker to receive and execute the93task. ::9495 $ celery -A your_application.celery worker9697The ``your_application`` string has to point to your application's package93or module that creates the `celery` object.98or module that creates the ``celery`` object.99100Now that the worker is running, ``wait`` will return the result once the task101is finished.