【问题标题】:Sample example from django-celery not working in Django app来自 django-celery 的示例示例在 Django 应用程序中不起作用
【发布时间】:2011-07-11 14:23:17
【问题描述】:

我正在学习这个教程

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

我用芹菜开始

python manage.py celeryd

然后我在myapp文件夹中创建了tasks.py

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

我错过了什么

【问题讨论】:

    标签: django celery django-celery


    【解决方案1】:

    在shell里面你是这样做的吗?

    from myapp import tasks
    

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

    tasks.add(4,4)
    

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

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

    【讨论】:

    • @user825904 没问题,很高兴我能帮上忙。
    猜你喜欢
    • 2016-07-17
    • 2014-04-05
    • 2013-12-08
    • 2013-07-31
    • 2016-11-09
    • 2018-08-09
    • 1970-01-01
    • 2013-12-11
    • 2013-06-21
    相关资源
    最近更新 更多