在烧瓶中启动芹菜:AttributeError:'Flask'对象没有属性'user_options' [英] Starting celery in flask: AttributeError: 'Flask' object has no attribute 'user_options'

查看:223
本文介绍了在烧瓶中启动芹菜:AttributeError:'Flask'对象没有属性'user_options'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  celery -A服务器应用程序工作者--loglevel = info 

代码和文件夹路径:

 server.py 
application / controllers / routes.py




$ b

  app =应用程序中的Flask(__ name__)
。控制器导入路由
app.run(host ='127.0.0.1',port = 5051,debug = True)

route.py

  from flask import Flask,
from celery import celery
from server import app


app.config ['CELERY_BROKER_URL'] ='redis:// localhost:6379/0'
app.config ['CELERY_RESULT_BACKEND'] ='redis:// localhost:6379/0'
$ b $ celery = celery(app.name,broker = app.config ['CELERY_BROKER_URL'])
celery。 conf.update(app.config)

@ celery.task()
def add_toge (自我,计数):
返回第一次成功

@ app.route(/ queing)
def testsfunction():
count = 1
add_together.delay(count)
returncool

追溯:

 追溯(最近一次调用最后):
文件/Library/Frameworks/Python.framework/Versions/2.7 / bin / celery,第11行,在< module>
sys.exit(main())
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/__main__.py,第30行,在
main()
文件中/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/bin/celery.py,第81行,主
cmd.execute_from_commandline(argv)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/bin/celery.py第770行,在execute_from_commandline
super(CeleryCommand,self).execute_from_commandline(argv)))
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages / celery / bin / base.py,第309行,在execute_from_commandline
argv = self.setup_app_from_commandline(argv)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2 (self.app.user_options ['preload'] or())$ b $ ./ / site-packages / celery / bin / base.py,第477行,在setup_app_from_commandline
user_preload = b AttributeError:'Flask'对象没有属性'user_options'

在终端中运行一个芹菜工。

解决方案

使用这个命令运行芹菜而不是你的:

  celery -A application.controllers.routes:celery worker --loglevel = info 

这将解决您当前的问题,但是您的代码有很多错误,例如,如果您希望在 add_together 函数你应该声明一个这样的任务:

  @ celery.task(bind = True)


I try to start a Celery worker server from a command line:

celery -A server application worker --loglevel=info  

The code and folder path:

server.py  
application/controllers/routes.py

server.py

app = Flask(__name__)  
from application.controllers import routes  
app.run(host='127.0.0.1',port=5051,debug=True)

route.py

from flask import Flask,  
from celery import Celery
from server import app 


app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0' 

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@celery.task()  
def add_together(self, count):  
   return "First success"

@app.route("/queing")  
def testsfunction():    
    count = 1  
    add_together.delay(count)  
    return "cool"

Trace back:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/__main__.py", line 30, in main
    main()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/bin/celery.py", line 770, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celery/bin/base.py", line 477, in setup_app_from_commandline
    user_preload = tuple(self.app.user_options['preload'] or ())
AttributeError: 'Flask' object has no attribute 'user_options'

I got this error when I'm running a celery worker in terminal.

解决方案

just run the celery with this command instead of yours:

celery -A application.controllers.routes:celery worker --loglevel=info

this will solve your current problem however your codes have a plenty of mistakes for example if you want to have a self argument inside your add_together function you you should declare a task like this:

@celery.task(bind=True)

这篇关于在烧瓶中启动芹菜:AttributeError:'Flask'对象没有属性'user_options'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆