celery.rst
This file
Path
docs/patterns/celery.rst
93 lines · 3.2 KiB
Last changed by
Point to stable version of CeleryMarkus Unterwaditzerbefore adoptionb471df6
History of this fileTake it
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/latest/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 :command:`pip` or :command:`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, backend=app.config['CELERY_BACKEND'],40 broker=app.config['CELERY_BROKER_URL'])41 celery.conf.update(app.config)42 TaskBase = celery.Task43 class ContextTask(TaskBase):44 abstract = True45 def __call__(self, *args, **kwargs):46 with app.app_context():47 return TaskBase.__call__(self, *args, **kwargs)48 celery.Task = ContextTask49 return celery5051The function creates a new Celery object, configures it with the broker52from the application config, updates the rest of the Celery config from53the Flask config and then creates a subclass of the task that wraps the54task execution in an application context.5556Minimal Example57---------------5859With what we have above this is the minimal example of using Celery with60Flask::6162 from flask import Flask6364 flask_app = Flask(__name__)65 flask_app.config.update(66 CELERY_BROKER_URL='redis://localhost:6379',67 CELERY_RESULT_BACKEND='redis://localhost:6379'68 )69 celery = make_celery(flask_app)707172 @celery.task()73 def add_together(a, b):74 return a + b7576This task can now be called in the background:7778>>> result = add_together.delay(23, 42)79>>> result.wait()80658182Running the Celery Worker83-------------------------8485Now if you jumped in and already executed the above code you will be86disappointed to learn that your ``.wait()`` will never actually return.87That's because you also need to run celery. You can do that by running88celery as a worker::8990 $ celery -A your_application.celery worker9192The ``your_application`` string has to point to your application's package93or module that creates the `celery` object.