Skip to content
Myr

celery.rst

This file

Path

docs/patterns/celery.rst

101 lines · 3.5 KiB

Last changed by

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

History of this file

Take it

The walk stopped after 200 commits: lines older than a228a3b point at it.

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 1–2why, one step back
1Celery Background Tasks
2=======================

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 3why, one step back
3

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 4–8why, one step back
4If your application has a long running task, such as processing some uploaded
5data 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.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 9why, one step back
9

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 10–14why, one step back
10Celery is a powerful task queue that can be used for simple background tasks
11as 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.

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 15why, one step back
15

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 16–17why, one step back
16Install
17-------

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 18why, one step back
18

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 19–20why, one step back
19Celery is a separate Python package. Install it from PyPI using pip::
20

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 21–22why, one step back
21 $ pip install celery
22

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 23–24why, one step back
23Configure
24---------

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 25–41why, one step back
25
26The 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 the29entry-point for everything you want to do in Celery, like creating tasks30and managing workers, it must be possible for other modules to import it.3132For instance you can place this in a ``tasks`` module. While you can use33Celery without any reconfiguration with Flask, it becomes a bit nicer by34subclassing tasks and adding support for Flask's application contexts and35hooking it up with the Flask configuration.3637This is all that is necessary to properly integrate Celery with Flask::3839 from celery import Celery4041 def make_celery(app):

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 42–46why, one step back
42 celery = Celery(
43 app.import_name,44 backend=app.config['CELERY_RESULT_BACKEND'],45 broker=app.config['CELERY_BROKER_URL']46 )

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 47why, one step back
47 celery.conf.update(app.config)

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 48–49why, one step back
48
49 class ContextTask(celery.Task):

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 50–51why, one step back
50 def __call__(self, *args, **kwargs):
51 with app.app_context():

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 52–53why, one step back
52 return self.run(*args, **kwargs)
53

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 54–61why, one step back
54 celery.Task = ContextTask
55 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.61

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

line 62why, one step back
62An example task

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 63–64why, one step back
63---------------
64

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 65–67why, one step back
65Let's write a task that adds two numbers together and returns the result. We
66configure 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. ::

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 68–81why, one step back
68
69 from flask import Flask7071 flask_app = Flask(__name__)72 flask_app.config.update(73 CELERY_BROKER_URL='redis://localhost:6379',74 CELERY_RESULT_BACKEND='redis://localhost:6379'75 )76 celery = make_celery(flask_app)7778 @celery.task()79 def add_together(a, b):80 return a + b81

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

line 82why, one step back
82This task can now be called in the background::

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 83why, one step back
83

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 84–85why, one step back
84 result = add_together.delay(23, 42)
85 result.wait() # 65

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 86why, one step back
86

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 87–88why, one step back
87Run a worker
88------------

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

line 89why, one step back
89

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 90–93why, one step back
90If you jumped in and already executed the above code you will be
91disappointed to learn that ``.wait()`` will never actually return.92That's because you also need to run a Celery worker to receive and execute the93task. ::

Merge pull request #1862 from antsar/masterJeff Widmanbefore adoptiona228a3b

lines 94–97why, one step back
94
95 $ celery -A your_application.celery worker9697The ``your_application`` string has to point to your application's package

Merge pull request #2055 from geoffreybauduin/celery-documentationDavid Lordbefore adoption88120e9

lines 98–101why, one step back
98or module that creates the ``celery`` object.
99100Now that the worker is running, ``wait`` will return the result once the task101is finished.