django-celery 的示例在 Django 应用程序中不起作用

发布于 2024-11-19 14:26:57 字数 1127 浏览 5 评论 0原文

我正在关注本教程

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 import jobs 中

一切顺利,

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

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

我缺少什么

I am following this tutorial

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

I startd the celery with

python manage.py celeryd

Then i made tasks.py in myapp folder with

from celery.decorators import task

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

Then i put these in 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 = "/"

Then i started the python shell with

python manage.py shell

Then i type

from myapp import tasks

It went ok

But when i type function name then i get error

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

What i am missing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-11-26 14:26:57

你在shell里面做了这个吗?

from myapp import tasks

如果是这样,您需要这样调用它:

tasks.add(4,4)

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

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

Inside of the shell did you do this?

from myapp import tasks

If so, you need to call it like this:

tasks.add(4,4)

Or you will need to change the import to the following:

from myapp.tasks import add
add(4,4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文