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
Provenance
- Commit
05f1623- Tree
699299f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
3fdcefb
2 files changed+93 added−0 removed
added:docs/patterns/celery.rst+92 added
1+Celery Based Background Tasks2+=============================3+4+Celery is a task queue for Python with batteries included. It used to5+have a Flask integration but it became unnecessary after some6+restructuring of the internals of Celery with Version 3. This guide fills7+in the blanks in how to properly use Celery with Flask but assumes that8+you generally already read the `First Steps with Celery9+<http://docs.celeryproject.org/en/master/getting-started/first-steps-with-celery.html>`_10+guide in the official Celery documentation.11+12+Installing Celery13+-----------------14+15+Celery is on the Python Package Index (PyPI), so it can be installed with16+standard Python tools like ``pip`` or ``easy_install``::17+18+ $ pip install celery19+20+Configuring Celery21+------------------22+23+The first thing you need is a Celery instance, this is called the celery24+application. It serves the same purpose as the :class:`~flask.Flask`25+object in Flask, just for Celery. Since this instance is used as the26+entry-point for everything you want to do in Celery, like creating tasks27+and managing workers, it must be possible for other modules to import it.28+29+For instance you can place this in a ``tasks`` module. While you can use30+Celery without any reconfiguration with Flask, it becomes a bit nicer by31+subclassing tasks and adding support for Flask's application contexts and32+hooking it up with the Flask configuration.33+34+This is all that is necessary to properly integrate Celery with Flask::35+36+ from celery import Celery37+38+ 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 celery49+50+The function creates a new Celery object, configures it with the broker51+from the application config, updates the rest of the Celery config from52+the Flask config and then creates a subclass of the task that wraps the53+task execution in an application context.54+55+Minimal Example56+---------------57+58+With what we have above this is the minimal example of using Celery with59+Flask::60+61+ from flask import Flask62+63+ 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)69+70+71+ @celery.task()72+ def add_together(a, b):73+ return a + b74+75+This task can now be called in the background:76+77+>>> result = add_together.delay(23, 42)78+>>> result.wait()79+6580+81+Running the Celery Worker82+-------------------------83+84+Now if you jumped in and already executed the above code you will be85+disappointed to learn that your ``.wait()`` will never actually return.86+That's because you also need to run celery. You can do that by running87+celery as a worker::88+89+ $ celery -A your_application worker90+91+The ``your_application`` string has to point to your application's package92+or module that creates the `celery` object.modified:docs/patterns/index.rst+1 added
37 unmodified lines38 streaming39 deferredcallbacks40 methodoverrides41 requestchecksum42+ celery