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
Provenance
- Commit
88120e9- Tree
f51013d- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
ff361d3- Parent 2
d5a88bf
1 file changed+44 added−36 removed
modified:docs/patterns/celery.rst+44 added-36 removed
1-Celery Based Background Tasks2-=============================1+Celery Background Tasks2+=======================3 4-Celery is a task queue for Python with batteries included. It used to5-have a Flask integration but it became unnecessary after some6-restructuring of the internals of Celery with Version 3. This guide fills7-in the blanks in how to properly use Celery with Flask but assumes that8-you generally already read the `First Steps with Celery9-<http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html>`_10-guide in the official Celery documentation.4+If your application has a long running task, such as processing some uploaded5+data or sending email, you don't want to wait for it to finish during a6+request. Instead, use a task queue to send the necessary data to another7+process that will run the task in the background while the request returns8+immediately.9 12-Installing Celery13------------------10+Celery is a powerful task queue that can be used for simple background tasks11+as well as complex multi-stage programs and schedules. This guide will show you12+how 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>`_14+guide in the Celery documentation.15 15-Celery is on the Python Package Index (PyPI), so it can be installed with16-standard Python tools like :command:`pip` or :command:`easy_install`::16+Install17+-------18 19+Celery is a separate Python package. Install it from PyPI using pip::20+21 $ pip install celery22 20-Configuring Celery21-------------------23+Configure24+---------25 26 The first thing you need is a Celery instance, this is called the celery27 application. It serves the same purpose as the :class:`~flask.Flask`28 object in Flask, just for Celery. Since this instance is used as the9 unmodified lines38 39 from celery import Celery40 41 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 = True48+49+ 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)53+54 celery.Task = ContextTask55 return celery56 57 The function creates a new Celery object, configures it with the broker58 from the application config, updates the rest of the Celery config from59 the Flask config and then creates a subclass of the task that wraps the60 task execution in an application context.61 56-Minimal Example62+An example task63 ---------------64 59-With what we have above this is the minimal example of using Celery with60-Flask::65+Let's write a task that adds two numbers together and returns the result. We66+configure Celery's broker and backend to use Redis, create a ``celery``67+application using the factor from above, and then use it to define the task. ::68 69 from flask import Flask70 71 flask_app = Flask(__name__)2 unmodified lines74 CELERY_RESULT_BACKEND='redis://localhost:6379'75 )76 celery = make_celery(flask_app)77 71-78 @celery.task()79 def add_together(a, b):80 return a + b81 76-This task can now be called in the background:82+This task can now be called in the background::83 78->>> result = add_together.delay(23, 42)79->>> result.wait()80-6584+ result = add_together.delay(23, 42)85+ result.wait() # 6586 82-Running the Celery Worker83--------------------------87+Run a worker88+------------89 85-Now if you jumped in and already executed the above code you will be86-disappointed to learn that your ``.wait()`` will never actually return.87-That's because you also need to run celery. You can do that by running88-celery as a worker::90+If you jumped in and already executed the above code you will be91+disappointed to learn that ``.wait()`` will never actually return.92+That's because you also need to run a Celery worker to receive and execute the93+task. ::94 95 $ celery -A your_application.celery worker96 97 The ``your_application`` string has to point to your application's package93-or module that creates the `celery` object.98+or module that creates the ``celery`` object.99+100+Now that the worker is running, ``wait`` will return the result once the task101+is finished.