我正在尝试在我的烧瓶示例应用程序中使用芹菜。因为我正在工厂方法中创建实例,所以我无法使用文档中的示例 (http://flask.pocoo.org/docs/0.10/patterns/celery/)
初始化.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')
我收到一个错误:
$ celery -A app.tasks worker
回溯(最近一次调用): 文件"...lib/python3.4/site-packages/celery/app/utils.py",第 229 行,find_app sym = symbol_by_name(app, imp=imp) 文件"...lib/python3.4/site-packages/celery/bin/base.py",第 488 行,symbol_by_name 返回symbol_by_name(名称,imp=imp) 文件"...lib/python3.4/site-packages/kombu/utils/__init__.py",第 97 行,symbol_by_name 返回 getattr(模块,cls_name) 如果cls_name else 模块属性错误:"模块"对象没有属性"任务"
-A 参数应指向要使用的 Celery 实例,而不是模块 http://docs.celeryproject.org/en/latest/reference/celery.bin.celery.html#cmdoption-celery-a
在这种情况下:
celery -A app.tasks.celery worker