skip to Main Content

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


  1. Try specifying task name explicitly, for example like this:

    @celery.task(name='app.celery.add')
    def add(x, y):
        return x + y
    

    To ensure changes takes effect, restart celery worker after you made changes and cleanup all *.pyc files with find . -name '*.pyc' -delete

    Login or Signup to reply.
  2. 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.

    [uwsgi]
    base = /home/project
    chdir = %(base)
    module = app
    pythonpath = %(base)
    virtualenv = %(base)/venv
    wsgi-file = %(base)/app.py
    master = true
    smart-attach-daemon = %(base)/tmp/celery.pid %(virtualenv)/bin/celery -A %(module).celery  worker --pidfile=%(base)/tmp/celery.pid
    socket = %(base)/socket.sock
    chmod-socket = 777
    processes = 4
    threads = 4
    logto = %(base)/log/%n.log
    stats = 127.0.0.1:9191
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search