forked from orbit-oss/flask
Merge pull request #4581 from lecovi/docs/#3871-celery-config
docs: new configuration format for celery
This commit is contained in:
commit
ab76703532
1 changed files with 15 additions and 11 deletions
|
|
@ -34,17 +34,15 @@ Celery without any reconfiguration with Flask, it becomes a bit nicer by
|
||||||
subclassing tasks and adding support for Flask's application contexts and
|
subclassing tasks and adding support for Flask's application contexts and
|
||||||
hooking it up with the Flask configuration.
|
hooking it up with the Flask configuration.
|
||||||
|
|
||||||
This is all that is necessary to properly integrate Celery with Flask::
|
This is all that is necessary to integrate Celery with Flask:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
from celery import Celery
|
from celery import Celery
|
||||||
|
|
||||||
def make_celery(app):
|
def make_celery(app):
|
||||||
celery = Celery(
|
celery = Celery(app.import_name)
|
||||||
app.import_name,
|
celery.conf.update(app.config["CELERY_CONFIG"])
|
||||||
backend=app.config['CELERY_RESULT_BACKEND'],
|
|
||||||
broker=app.config['CELERY_BROKER_URL']
|
|
||||||
)
|
|
||||||
celery.conf.update(app.config)
|
|
||||||
|
|
||||||
class ContextTask(celery.Task):
|
class ContextTask(celery.Task):
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|
@ -59,6 +57,12 @@ from the application config, updates the rest of the Celery config from
|
||||||
the Flask config and then creates a subclass of the task that wraps the
|
the Flask config and then creates a subclass of the task that wraps the
|
||||||
task execution in an application context.
|
task execution in an application context.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
Celery 5.x deprecated uppercase configuration keys, and 6.x will
|
||||||
|
remove them. See their official `migration guide`_.
|
||||||
|
|
||||||
|
.. _migration guide: https://docs.celeryproject.org/en/stable/userguide/configuration.html#conf-old-settings-map.
|
||||||
|
|
||||||
An example task
|
An example task
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
|
|
@ -69,10 +73,10 @@ application using the factory from above, and then use it to define the task. ::
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
flask_app = Flask(__name__)
|
flask_app = Flask(__name__)
|
||||||
flask_app.config.update(
|
flask_app.config.update(CELERY_CONFIG={
|
||||||
CELERY_BROKER_URL='redis://localhost:6379',
|
'broker_url': 'redis://localhost:6379',
|
||||||
CELERY_RESULT_BACKEND='redis://localhost:6379'
|
'result_backend': 'redis://localhost:6379',
|
||||||
)
|
})
|
||||||
celery = make_celery(flask_app)
|
celery = make_celery(flask_app)
|
||||||
|
|
||||||
@celery.task()
|
@celery.task()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue