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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25884951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:47:12  来源:igfitidea点击:

AttributeError: 'Flask' object has no attribute 'user_options'

pythonflaskcelery

提问by Ravdeep

I am trying to setup this basic example from the following doc:

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

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

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

But so far I keep getting the below error:

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

AttributeError: 'Flask' object has no attribute 'user_options'

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

I am using celery 3.1.15.

我正在使用芹菜 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

Example:

例子:

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 error:

追溯错误:

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'

采纳答案by TomL

The Flask Celery Based Background Tasks page (http://flask.pocoo.org/docs/patterns/celery/) suggests this to start celery:

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

celery -A your_application worker

celery -A your_application worker

The your_application string has to point to your application's package or module that creates the celery object.

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

Assuming the code resides in application.py, explicitly pointing to the celery object (not just the module name) avoided the error:

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

celery -A application.celery worker

celery -A application.celery worker

回答by nhywieza

rename app flask_app It will work

重命名应用程序flask_app 它将起作用

回答by user7374381

like this:

像这样:

celery -A your_application worker

where your_application stands:

your_application 所在的位置:

your_application = Flask(\__name\__)

the python file name: your_application.py, it will work

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

By the way, celery v4 is unsupported in Windows

顺便说一句,芹菜 v4 在 Windows 中不受支持

回答by sri

This worked for me:

这对我有用:

celery -A my_app_module_name.celery worker