且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django Celery应用程序-没有名为celery的模块错误

更新时间:2023-11-20 16:59:28

编辑2014年4月:

Celery文档已更新为3.1;以下解决方案现在已过时,请参见:

The Celery docs have been updated for 3.1; the below solution is now outdated, see:

http://docs.celeryproject.org/en/master/django/first-steps-with-django.html

默认情况下,celery搜索名为 celery.py 的模块以查找其配置。通过指定celery.py 不同的名称。 /next-steps.html#about-the-app-argument rel = noreferrer>在应用程序参数上-在此示例中,我们将在设置中查找celery配置.py

By default, celery searches for a module named celery.py to find its configuration. You can get celery to use a different name than celery.py by specify it on the app argument - in this example, we'll look for celery config in settings.py:

python manage.py celery worker --app=myapp.settings

使用 django-celery 时,您可以使用上面的调用以开始celery,或者像我最初所做的那样在我的应用程序包 myapp 中创建一个 celery.py

When using django-celery you can either use the above call to start celery, or do as I originally did and create a celery.py in my application package myapp:

from settings import celery

我的Django settings.py 包含常规的celery配置:

My Django settings.py contains the normal celery config:

from celery import Celery

celery = Celery(broker="amqp://guest:guest@127.0.0.1:5672//")

celery.conf.update(
    CELERY_DEFAULT_QUEUE = "myapp",
    CELERY_DEFAULT_EXCHANGE = "myapp",
    CELERY_DEFAULT_EXCHANGE_TYPE = "direct",
    CELERY_DEFAULT_ROUTING_KEY = "myapp",
)

然后像这样运行芹菜工人:

Then run the celery worker like this:

python manage.py celery worker --app=myapp

为清楚起见,这是我的完整应用程序结构:

Just for clarity's sake, here's my full application structure:

myproject/
    manage.py
    myapp/
        __init__.py
        settings.py
        celery.py