164

I'm trying to run example from Celery documentation.

I run: celeryd --loglevel=INFO

/usr/local/lib/python2.7/dist-packages/celery/loaders/default.py:64: NotConfigured: No 'celeryconfig' module found! Please make sure it exists and is available to Python.
  "is available to Python." % (configname, )))
[2012-03-19 04:26:34,899: WARNING/MainProcess]  

 -------------- celery@ubuntu v2.5.1
---- **** -----
--- * ***  * -- [Configuration]
-- * - **** ---   . broker:      amqp://guest@localhost:5672//
- ** ----------   . loader:      celery.loaders.default.Loader
- ** ----------   . logfile:     [stderr]@INFO
- ** ----------   . concurrency: 4
- ** ----------   . events:      OFF
- *** --- * ---   . beat:        OFF
-- ******* ----
--- ***** ----- [Queues]
 --------------   . celery:      exchange:celery (direct) binding:celery

tasks.py:

# -*- coding: utf-8 -*-
from celery.task import task

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

run_task.py:

# -*- coding: utf-8 -*-
from tasks import add
result = add.delay(4, 4)
print (result)
print (result.ready())
print (result.get())

In same folder celeryconfig.py:

CELERY_IMPORTS = ("tasks", )
CELERY_RESULT_BACKEND = "amqp"
BROKER_URL = "amqp://guest:guest@localhost:5672//"
CELERY_TASK_RESULT_EXPIRES = 300

When I run "run_task.py":

on python console

eb503f77-b5fc-44e2-ac0b-91ce6ddbf153
False

errors on celeryd server

[2012-03-19 04:34:14,913: ERROR/MainProcess] Received unregistered task of type 'tasks.add'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'retries': 0, 'task': 'tasks.add', 'utc': False, 'args': (4, 4), 'expires': None, 'eta': None, 'kwargs': {}, 'id': '841bc21f-8124-436b-92f1-e3b62cafdfe7'}

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer.py", line 444, in receive_message
    self.strategies[name](message, body, message.ack_log_error)
KeyError: 'tasks.add'

Please explain what's the problem.

3
  • 16
    Hi, could you please share what the problem was and how you resolved? The accepted answer doesn't make it clear how others could solve this problem. Thanks. Commented Apr 17, 2012 at 22:30
  • 4
    I'm with Jordan- this was not useful at all. Downvoted.
    – Jay Taylor
    Commented Aug 24, 2012 at 1:06
  • 3
    the answer of aiho is the correct one: CELERY_IMPORTS = ("tasks", )
    – Alp
    Commented Nov 8, 2014 at 19:35

42 Answers 42

1
2
0

Write the correct path to the file tasks

app.conf.beat_schedule = {
'send-task': {
    'task': 'appdir.tasks.testapp',
    'schedule': crontab(minute='*/5'),  
},

}

0

when running the celery with "celery -A conf worker -l info" command all the tasks got listed in log like i was having . conf.celery.debug_task i was getting the error because I was not giving this exact task path. So kindly recheck this by copying and pasting exact task id.

0
app = Celery(__name__, broker=app.config['CELERY_BROKER'], 
backend=app.config['CELERY_BACKEND'], include=['util.xxxx', 'util.yyyy'])
0
0

The answer to your problem lies in THE FIRST LINE of the output you provided in your question: /usr/local/lib/python2.7/dist-packages/celery/loaders/default.py:64: NotConfigured: No 'celeryconfig' module found! Please make sure it exists and is available to Python. "is available to Python." % (configname, ))). Without the right configuration Celery is not able to do anything.

Reason why it can't find the celeryconfig is most likely it is not in your PYTHONPATH.

0

This solved my issue (put it inside your create_app() function):

celery.conf.update(app.config)

class ContextTask(celery.Task):
    def __call__(self, *args, **kwargs):
        with app.app_context():
            return self.run(*args, **kwargs)

celery.Task = ContextTask
0

if you're using Docker, like said @ here will kill your pain.

docker stop $(docker ps -a -q)
3
  • 1
    If you are using docker or docker-compose this is the answer. Re-build, for some reason, it doesn't work quite right. I have my suspicions why, but not the time to explore them. Not just restart, rebuild.
    – ThatGuyRob
    Commented Dec 7, 2021 at 20:10
  • Probably, your app context and celery worker's context don't match. Using celery with 3 different frameworks taught me the real reason. :D
    – AgE
    Commented Jun 14, 2022 at 17:26
  • This also stops all docker containers...
    – BlakBat
    Commented Aug 24, 2023 at 19:20
0

For me, restarting the broker (Redis) solved it.


The task already showed up correctly in Celery's task list and all relevant Django settings and imports worked fine.

My broker was running before I wrote the task, and restarting Celery and Django alone didn't solve it.

However, stopping Redis with Ctrl+C and then restarting it with redis-server helped Celery to correctly identify the task.

0

This error was encountered because Celery did not find the config. As of 2024, I got it solved by doing the following. I added the following settings to my config in my celeryconfig.py.

broker_url = 'redis:localhost:6379/0'
result_backend = 'redis://localhost:6379/0'
task_serializer = 'json'
result_serializer = 'json'
imports = ("src.v1.tasks",)
broker_connection_retry_on_startup =True

I finally ran my celery app with

celery -A runserver.celery --config=celeryconfig worker --loglevel=INFO

note that I am pointing to where my config is located using the --config option. Also, I am running my app by getting the module runserver.py which has my celery object. the imports settings will point to where my tasks are located, making it easy for Celery to locate them.

Hope it helps.

0

Apparently, Celery dropped the support for automatically registering Task classes. https://docs.celeryq.dev/en/latest/history/whatsnew-4.0.html#the-task-base-class-no-longer-automatically-register-tasks

Also this has caused a syntax change. Normally, I would need to instantiate the class before calling the delay method. Now, i have to call it directly and the IDE is not happy with that. Also, instead of EmailVerificationTask().run(...), now I do EmailVerificationTask(...). I'm trying to figure out if I'm missing something here.

EmailVerificationTask = celery_app.register_task(EmailVerificationTask)
EmailVerificationTask.delay(user_email="a", verification_link="b")
1
  • We just started receiving this message and didn't update anything, what have changed?
    – Anton Kim
    Commented Jun 26 at 0:23
-1

In my case, the wrong task name had already been persisted by celery beat... was still early enough for me to nuke everything.

-1

I was getting same kind of error in Flask when I was running the server using python app.py. In order to solve it, I ran the server using flask run

-1

After searching for wholeday, finally got working after cleaning .pyc files

py3clean .
1
2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.