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 fileTake it
The walk stopped after 200 commits: lines older than bd51d2b point at it.
resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 1–2why, one step back1Celery Background Tasks2=======================Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 3why, one step back3resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 4–8why, one step back4If 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.Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 9why, one step back9resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 10–14why, one step back10Celery 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.Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 15why, one step back15resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 16–17why, one step back16Install17-------Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 18why, one step back18resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 19–20why, one step back19Celery is a separate Python package. Install it from PyPI using pip::20Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 21–22why, one step back21 $ pip install celery22resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 23–24why, one step back23Configure24---------Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 25–41why, one step back2526The 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 back42 celery = Celery(43 app.import_name,44 backend=app.config['CELERY_RESULT_BACKEND'],45 broker=app.config['CELERY_BROKER_URL']46 )Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 47why, one step back47 celery.conf.update(app.config)resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 48–49why, one step back4849 class ContextTask(celery.Task):Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 50–51why, one step back50 def __call__(self, *args, **kwargs):51 with app.app_context():resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 52–53why, one step back52 return self.run(*args, **kwargs)53Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 54–61why, one step back54 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.61resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 62why, one step back62An example taskMerge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 63–64why, one step back63---------------64resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 65–67why, one step back65Let'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. ::Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 68–81why, one step back6869 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 + b81resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 82why, one step back82This task can now be called in the background::Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 83why, one step back83resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 84–85why, one step back84 result = add_together.delay(23, 42)85 result.wait() # 65Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 86why, one step back86resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 87–88why, one step back87Run a worker88------------Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 89why, one step back89resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 90–93why, one step back90If 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. ::Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 94–97why, one step back9495 $ celery -A your_application.celery worker9697The ``your_application`` string has to point to your application's packageresolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 98–101why, one step back98or module that creates the ``celery`` object.99100Now that the worker is running, ``wait`` will return the result once the task101is finished.