[Fixed]-Why does Celery work in Python shell, but not in my Django views? (import problem)

10đź‘Ť

âś…

I believe your tasks.py file needs to be in a django app (that’s registered in settings.py) in order to be imported. Alternatively, you might try importing the tasks from an __init__.py file in your main project or one of the apps.

Also try starting celeryd from manage.py:

$ python manage.py celeryd -E -B -lDEBUG

(-E and -B may or may not be necessary, but that’s what I use).

👤Seth

11đź‘Ť

This is what I did which finally worked

in Settings.py I added

CELERY_IMPORTS = ("myapp.jobs", )

under myapp folder I created a file called jobs.py

from celery.decorators import task

@task(name="jobs.add")
def add(x, y):
    return x * y

Then ran from commandline: python manage.py celeryd -l info

in another shell i ran python manage.py shell, then

>>> from myapp.jobs import add
>>> result = add.delay(4, 4)
>>> result.result

and the i get:

16

The important point is that you have to rerun both command shells when you add a new function. You have to register the name both on the client and and on the server.

🙂

👤David Dehghan

5đź‘Ť

See Automatic Naming and Relative Imports, in the docs:

http://celeryq.org/docs/userguide/tasks.html#automatic-naming-and-relative-imports

The tasks name is “tasks.Submitter” (as listed in the celeryd output),
but you import the task as “fable.jobs.tasks.Submitter”

I guess the best solution here is if the worker also sees it as “fable.jobs.tasks.Submitter”,
it makes more sense from an app perspective.

CELERY_IMPORTS = ("fable.jobs.tasks", )
👤asksol

Leave a comment