I am trying to use Celery in my Flask Example application. Because I am creating instance in Factory method I can not use example from documentation (http://flask.pocoo.org/docs/0.10/patterns/celery/)
init.py
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 am getting an 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'
The -A param should point to the instance of Celery to use, not the module http://docs.celeryproject.org/en/latest/reference/celery.bin.celery.html#cmdoption-celery-a
In this case:
celery -A app.tasks.celery worker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With