Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting celery in flask: AttributeError: 'Flask' object has no attribute 'user_options'

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.

like image 532
prasanth Avatar asked Dec 17 '15 11:12

prasanth


1 Answers

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)
like image 51
mehdy Avatar answered Sep 20 '22 09:09

mehdy