I have a simple application flask with a simple celery task:
from flask import Flask
from celery import Celery
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@celery.task
def add(x, y):
return x + y
@app.route('/', methods=['GET'])
def test_func():
res = add.delay(4,5)
while not res.ready():
pass
data = res.get()
return str(data)
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
When I try to use your exemple with uwsgi but I encounter almost the same error than you.
First, I run it simply with python:
python app.py
and the broker with the following command:
celery -A app.celery worker -l info
Everything is working perfectly. Now I try to launch the flask application with uwsgi.
[uwsgi]
mount = /=/home/admin/flask-celery/app.py
callable = app
virtualenv = /home/admin/flask-celery/.venv
socket = :3031
master = true
processes = 2
threads = 4
http = :9000
But when I go on my route, I encounter the following error:
celery.exceptions.NotRegistered: 'uwsgi_file__home_admin_flask-celery_app.add'
2
Answers
Try specifying task name explicitly, for example like this:
To ensure changes takes effect, restart celery worker after you made changes and cleanup all
*.pyc
files withfind . -name '*.pyc' -delete
Pleas take a look at this:https://uwsgi-docs.readthedocs.io/en/latest/AttachingDaemons.html
Although the following configuration does not exactly match your environment, I hope this could help.