AttributeError:' Flask'对象没有属性' user_options'

时间:2014-09-17 07:42:10

标签: python flask celery

我正在尝试从以下文档设置此基本示例:

  

http://flask.pocoo.org/docs/patterns/celery/

但到目前为止,我一直收到以下错误:

  

AttributeError:'Flask'对象没有属性'user_options'

我正在使用芹菜3.1.15。

from celery import Celery

def make_celery(app):
    celery = Celery(app.import_name, 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

示例:

from flask import Flask

app = Flask(__name__)
app.config.update(
    CELERY_BROKER_URL='redis://localhost:6379',
    CELERY_RESULT_BACKEND='redis://localhost:6379'
)
celery = make_celery(app)


@celery.task()
def add_together(a, b):
    return a + b

回溯错误:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
    main()
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 305, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 473, in setup_app_from_commandline
    user_preload = tuple(self.app.user_options['preload'] or ())
AttributeError: 'Flask' object has no attribute 'user_options'

4 个答案:

答案 0 :(得分:79)

基于Flask Celery的后台任务页面(http://flask.pocoo.org/docs/patterns/celery/)建议这可以启动芹菜:

celery -A your_application worker

  

your_application字符串必须指向创建celery对象的应用程序包或模块。

假设代码驻留在application.py中,显式指向celery对象(而不仅仅是模块名称)避免了错误:

celery -A application.celery worker

答案 1 :(得分:1)

这对我有用:

celery -A my_app_module_name.celery worker

答案 2 :(得分:-3)

重命名app flask_app 它会起作用

答案 3 :(得分:-3)

像这样:

celery -A your_application worker

your_application代表:

your_application = Flask(\__name\__)

python文件名: your_application .py,它会起作用

顺便说一下,Windows中不支持celery v4