Skip to content
Myr

docs: new configuration format for celery

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

authored by lecovi · committed by David Lord · parent d506af1 · Browse files · 1 file+15 added−11 removed
This commit

State

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

Diff stats

1 file+15 added−11 removed

  • other:+15 added−11 removed

1 file+15 added−11 removed

modified:docs/patterns/celery.rst+15 added-11 removed
32 unmodified lines
33Celery 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::37This is all that is necessary to integrate Celery with Flask:3839.. code-block:: python4041 from celery import Celery4243 def make_celery(app):42 celery = Celery(43 app.import_name,44 backend=app.config['CELERY_RESULT_BACKEND'],45 broker=app.config['CELERY_BROKER_URL']46 )47 celery.conf.update(app.config)44 celery = Celery(app.import_name)45 celery.conf.update(app.config["CELERY_CONFIG"])4647 class ContextTask(celery.Task):48 def __call__(self, *args, **kwargs):49 with app.app_context():
6 unmodified lines
56from the application config, updates the rest of the Celery config from57the Flask config and then creates a subclass of the task that wraps the58task execution in an application context.5960.. note::61 Celery 5.x deprecated uppercase configuration keys, and 6.x will62 remove them. See their official `migration guide`_.6364.. _migration guide: https://docs.celeryproject.org/en/stable/userguide/configuration.html#conf-old-settings-map.6566An example task67---------------6869Let's write a task that adds two numbers together and returns the result. We
2 unmodified lines
7273 from flask import Flask7475 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 flask_app.config.update(CELERY_CONFIG={77 'broker_url': 'redis://localhost:6379',78 'result_backend': 'redis://localhost:6379',79 })80 celery = make_celery(flask_app)8182 @celery.task()83 def add_together(a, b):
22 unmodified lines