Skip to content
Myr

Added celery pattern

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 3fdcefb · Browse files · 2 files+93 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+93 added−0 removed

  • other:+93 added−0 removed

2 files+93 added−0 removed

added:docs/patterns/celery.rst+92 added
1Celery 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 with16standard Python tools like ``pip`` or ``easy_install``::1718 $ 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 Flask6263 app = Flask(__name__)64 app.config.update(65 CELERY_BROKER_URL='redis://localhost:6379',66 CELERY_RESULT_BACKEND='redis://localhost:6379'67 )68 celery = make_celery(app)697071 @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::8889 $ celery -A your_application worker9091The ``your_application`` string has to point to your application's package92or module that creates the `celery` object.
modified:docs/patterns/index.rst+1 added
37 unmodified lines
38 streaming39 deferredcallbacks40 methodoverrides41 requestchecksum42 celery