59

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

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

But so far I keep getting the below error:

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

I am using celery 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'
7
  • Please show us the full traceback of the error. Commented Sep 17, 2014 at 7:42
  • 1
    You need to edit your question to add that. Commented Sep 17, 2014 at 7:46
  • 1
    What command line did you use to run Celery? How did you configure Celery itself? You only showed us the Flask integration side. Commented Sep 17, 2014 at 7:50
  • so I am using "celery -A <my_application> worker" to run the celery. Commented Sep 17, 2014 at 7:52
  • I am not configuring anything on celery side.I am just following that documentation directly. Commented Sep 17, 2014 at 8:00

5 Answers 5

128

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

celery -A your_application worker

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

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

celery -A application.celery worker

Sign up to request clarification or add additional context in comments.

5 Comments

I've been running into a number of issues of this kind. Celery's a bit magical for my tastes.
I got same problem today... My issue was app = Flask(__name__) because it clash with celery internal self.app. So you just need to rename app = Flask(__name__) to "application" or something else to avoid this traceback
This worked for me 🚀️
I love Celery but the docs leave MUCH to be desired. I suppose they only cater to veteran network and web programmers... Kinda hostile toward newbs.
For me, I was following this instruction link for celery on Flask: flask.palletsprojects.com/en/3.0.x/patterns/celery so I changed from celery -A example worker to celery -A example.celery_app worker to make it work.
11

This worked for me:

celery -A my_app_module_name.celery worker

1 Comment

For me as well. I use celery 4.3.0 (rhubarb)
0

rename app flask_app It will work

2 Comments

This is irrelevant. The OP is not struggling with any NameError and your answer is quite vague how it could solve the problem (which it seems it doesn't anyway).
@Reti43 if the file name is app.py, then app = Flask(__name__) will have a conflict. To resolve this, he suggested renaming the app to flask_app. I agree that @nhywieza did not give the proper context.
0

Celery is trying to run wrong app it's Flask instance
solotion1: rename app -> flask_app or other name
solution2: specify Celery instance celery -A my_app_module_name.celery worker (added .celery)

Comments

-4

like this:

celery -A your_application worker

where your_application stands:

your_application = Flask(\__name\__)

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

By the way, celery v4 is unsupported in Windows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.