celery tutorial: NotRegistered error

Following Will solve your Problem

from tasks import add
res = add.delay(1,2) #call to add 
res.get() #get result

Restart your worker after changes using

celery -A tasks worker --loglevel=info

try to import tasks first, I recommend you implement your work in a interactive python environment, like a python IDE, and then you do this:

  • import tasks

before you write tasks.add


Had the same problem; I found solution by comparing PYTHONPATH from my main.py and the one from console where I run my celery worker. tasks.py was not in PYTHONPATH for the celery worker so it couldn't import the task.

my files:

try_celery/tasks.py

from celery import Celery

app = Celery('tasks', broker='pyamqp://guest@localhost//', backend='redis://localhost')


@app.task
def add(x, y):
    return x + y

try_celery/main.py

from try_celery.tasks import add

if __name__ == '__main__':
    result = add.delay(4, 4)
    while not result.ready():
        pass
    print(result.get())

run_worker.sh

#!/bin/bash
PYTHONPATH=$PYTHONPATH:/<path_to_project>  # line this solved the problem
celery -A try_celery.tasks worker --loglevel=info

to execute, first run run_worker.sh then run python3 main.py As I run my main.py from IDE, the PYTHONPATH was already updated with project path

Tags:

Python

Celery