django-celery的示例不适用于Django应用程序

时间:2011-07-11 14:23:18

标签: django celery django-celery

我正在学习本教程

http://celeryq.org/docs/django-celery/getting-started/first-steps-with-django.html

我用

开始了芹菜

python manage.py celeryd

然后我使用

tasks.py文件夹中myapp
from celery.decorators import task

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

然后我把它们放在settings.py

import djcelery
djcelery.setup_loader()

    CELERY_RESULT_BACKEND = "database"
    CELERY_RESULT_DBURI = "mysql://user1:password@localhost/ajfdfa_rabbitmq"

    BROKER_HOST = "localhost"
    BROKER_PORT = 5672
    BROKER_USER = "guest"
    BROKER_PASSWORD = "guest"
    BROKER_VHOST = "/"

然后我用

启动了python shell

python manage.py shell

然后我输入

来自myapp导入任务

没关系

但是当我键入函数名称时,我会收到错误

add.delay(4, 4)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'add' is not defined

我缺少什么

1 个答案:

答案 0 :(得分:3)

在shell中你做到了吗?

from myapp import tasks

如果是这样,你需要这样称呼它:

tasks.add(4,4)

或者您需要将导入更改为以下内容:

from myapp.tasks import add
add(4,4)