celery.rst
This file
Path
docs/patterns/celery.rst
92 lines · 3.1 KiB
Last changed by
Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
History of this fileTake it
The walk stopped after 200 commits: lines older than 05161d3 point at it.
Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 1–15why, one step back1Celery Based 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/master/getting-started/first-steps-with-celery.html>`_10guide in the official Celery documentation.1112Installing Celery13-----------------1415Celery is on the Python Package Index (PyPI), so it can be installed withMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 16why, one step back16standard Python tools like :command:`pip` or :command:`easy_install`::Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 17–62why, one step back1718 $ pip install celery1920Configuring Celery21------------------2223The first thing you need is a Celery instance, this is called the celery24application. It serves the same purpose as the :class:`~flask.Flask`25object in Flask, just for Celery. Since this instance is used as the26entry-point for everything you want to do in Celery, like creating tasks27and managing workers, it must be possible for other modules to import it.2829For instance you can place this in a ``tasks`` module. While you can use30Celery without any reconfiguration with Flask, it becomes a bit nicer by31subclassing tasks and adding support for Flask's application contexts and32hooking it up with the Flask configuration.3334This is all that is necessary to properly integrate Celery with Flask::3536 from celery import Celery3738 def make_celery(app):39 celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])40 celery.conf.update(app.config)41 TaskBase = celery.Task42 class ContextTask(TaskBase):43 abstract = True44 def __call__(self, *args, **kwargs):45 with app.app_context():46 return TaskBase.__call__(self, *args, **kwargs)47 celery.Task = ContextTask48 return celery4950The function creates a new Celery object, configures it with the broker51from the application config, updates the rest of the Celery config from52the Flask config and then creates a subclass of the task that wraps the53task execution in an application context.5455Minimal Example56---------------5758With what we have above this is the minimal example of using Celery with59Flask::6061 from flask import Flask62Merge pull request #931 from aphexcx/patch-1Armin Ronacherbefore adoptione95cef6
lines 63–64why, one step back63 flask_app = Flask(__name__)64 flask_app.config.update(Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 65–67why, one step back65 CELERY_BROKER_URL='redis://localhost:6379',66 CELERY_RESULT_BACKEND='redis://localhost:6379'67 )Merge pull request #931 from aphexcx/patch-1Armin Ronacherbefore adoptione95cef6
line 68why, one step back68 celery = make_celery(flask_app)Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 69–88why, one step back697071 @celery.task()72 def add_together(a, b):73 return a + b7475This task can now be called in the background:7677>>> result = add_together.delay(23, 42)78>>> result.wait()79658081Running the Celery Worker82-------------------------8384Now if you jumped in and already executed the above code you will be85disappointed to learn that your ``.wait()`` will never actually return.86That's because you also need to run celery. You can do that by running87celery as a worker::88Merge pull request #972 from alexpearce/patch-1Armin Ronacherbefore adoption11003d0
line 89why, one step back89 $ celery -A your_application.celery workerRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 90–92why, one step back9091The ``your_application`` string has to point to your application's package92or module that creates the `celery` object.