Combining flasks with celery

I am trying to use celery in my checkbox example . Since I create an instance in the Factory method, I can not use the example from the documentation ( http://flask.pocoo.org/docs/0.10/patterns/celery/ )

__ __ INIT. RU

from celery import Celery from flask import Flask from config import config def create_app(): app = Flask(__name__) app.debug = True app.config.from_object(config) from .main import main as main_blueprint app.register_blueprint(main_blueprint) return app def make_celery(app = None): app = app or create_app() celery = Celery('app', backend=app.config['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery 

tasks.py

 from app import make_celery celery = make_celery() @celery.task def add(a, b): return a + b 

views.py

 from flask import render_template from app.main import main from ..tasks import add @main.route('/', methods=['GET', 'POST']) def index(): add.delay(5, 3) return render_template('index.html') 

I get the error :

 $ celery -A app.tasks worker 
  Traceback (most recent call last):
   File "... lib / python3.4 / site-packages / celery / app / utils.py", line 229, in find_app
     sym = symbol_by_name (app, imp = imp)
   File "... lib / python3.4 / site-packages / celery / bin / base.py", line 488, in symbol_by_name
     return symbol_by_name (name, imp = imp)
   File "... lib / python3.4 / site-packages / kombu / utils / __ init__.py", line 97, in symbol_by_name
     return getattr (module, cls_name) if cls_name else module
 AttributeError: 'module' object has no attribute 'tasks'
+6
source share
1 answer

The -A parameter must point to the Celery instance to use, and not to the http://docs.celeryproject.org/en/latest/reference/celery.bin.celery.html#cmdoption-celery-a module

In this case:

 celery -A app.tasks.celery worker 
0
source

Source: https://habr.com/ru/post/979573/


All Articles