Skip to content
Myr

celery.rst

This file

Path

docs/patterns/celery.rst

101 lines · 3.5 KiB

Last changed by

resolve merge conflictsKenneth Reitzbefore adoption31174fe

History of this file

Take it

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 3why, one step back
3

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 9why, one step back
9

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 15why, one step back
15

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 18why, one step back
18

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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):

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 62why, one step back
62An example task

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 83why, one step back
83

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 86why, one step back
86

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Merge pull request #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

line 89why, one step back
89

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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 #1185 from defuz/patch-2Markus Unterwaditzerbefore adoption7550f43

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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.