django 项目中的 Celery 异步任务。它是如何运作的?

发布于 2024-11-15 20:21:09 字数 1124 浏览 4 评论 0原文

我需要在我的 Django 项目中运行长时间的任务。决定使用 celery 和 redis 作为代理。安装的redis运行:

服务器现在已准备好接受端口 6379 上的连接

比我安装 django-celery、配置:

import djcelery
djcelery.setup_loader()
BROKER_HOST = "localhost"
BROKER_PORT = 6379 #redis
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"

并运行它:

python manage.py celeryd -l DEBUG
[...]
[2011-06-18 10:31:37,913: DEBUG/MainProcess] Starting thread Timer...
[2011-06-18 10:31:37,914: DEBUG/MainProcess] Starting thread Consumer... 
[2011-06-18 10:31:37,914: WARNING/MainProcess] celery@greg... has started.
[2011-06-18 10:31:37,914: DEBUG/MainProcess] Consumer: Re-establishing connection to the broker...

我的示例任务如下所示:

from celery.decorators import task
@task()
def add(x, y):
    return x + y

现在我尝试在 shell 中运行它:

In [3]: from message.tasks import add
In [4]: r=add.delay(2, 5)    

它等待很长时间并呈现 Traceback http://dpaste.com/555939/。它可以是什么?也许我错过了什么?

I need in my django project run long tasks. Desided to use celery with redis as broker. Installed redis runs:

The server is now ready to accept connections on port 6379

Than I install django-celery, configure:

import djcelery
djcelery.setup_loader()
BROKER_HOST = "localhost"
BROKER_PORT = 6379 #redis
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"

and run it:

python manage.py celeryd -l DEBUG
[...]
[2011-06-18 10:31:37,913: DEBUG/MainProcess] Starting thread Timer...
[2011-06-18 10:31:37,914: DEBUG/MainProcess] Starting thread Consumer... 
[2011-06-18 10:31:37,914: WARNING/MainProcess] celery@greg... has started.
[2011-06-18 10:31:37,914: DEBUG/MainProcess] Consumer: Re-establishing connection to the broker...

my example task look like:

from celery.decorators import task
@task()
def add(x, y):
    return x + y

now I try to run it in shell:

In [3]: from message.tasks import add
In [4]: r=add.delay(2, 5)    

it wait very long and render Traceback http://dpaste.com/555939/. What it can be? Maybe I miss something?

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

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

发布评论

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

评论(2

原野 2024-11-22 20:21:09

缺少 BROKER_BACKEND 设置。以下是使用 Redis 的示例配置:

import djcelery
djcelery.setup_loader()

CELERY_RESULT_BACKEND = 'database'

BROKER_BACKEND = 'redis'
BROKER_HOST = 'localhost'
BROKER_PORT = 6379
BROKER_VHOST = '1'

The BROKER_BACKEND setting is missing. Here is a example config for using Redis:

import djcelery
djcelery.setup_loader()

CELERY_RESULT_BACKEND = 'database'

BROKER_BACKEND = 'redis'
BROKER_HOST = 'localhost'
BROKER_PORT = 6379
BROKER_VHOST = '1'
辞别 2024-11-22 20:21:09

不知道这是什么,但我知道 RabbitMQ 是 Celery 和 Django 的推荐代理。我让它运行起来,它就像一个魅力。为什么不尝试一下呢?

Don't know what this is, but I do know that RabbitMQ is the recommended broker for Celery and Django. I have it running and it works like a charm. Why not give that one a go?

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