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

后端 未结 3 2102
渐次进展
渐次进展 2020-12-29 08:47

I installed Celery (latest stable version.) I have a directory called /home/myuser/fable/jobs. Inside this directory, I have a file called tasks.py:

         


        
相关标签:
3条回答
  • 2020-12-29 09:05

    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.

    :-)

    0 讨论(0)
  • 2020-12-29 09:08

    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).

    0 讨论(0)
  • 2020-12-29 09:08

    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", )
    
    0 讨论(0)
提交回复
热议问题