python - django 项目中的 Celery 异步任务。这个怎么运作?

标签 python django redis celery django-celery

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

The server is now ready to accept connections on port 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/ .它可以是什么?也许我错过了什么?

最佳答案

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

import djcelery
djcelery.setup_loader()

CELERY_RESULT_BACKEND = 'database'

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

关于python - django 项目中的 Celery 异步任务。这个怎么运作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6397094/

相关文章:

php - 使用 Redis 缓存 Eloquent 模型实例

asp.net-mvc - Redis 缓存不适用于 Asp.net 核心

python , NumPy ;如何在数组的开头插入元素

python - 更改plotlys悬停提示文本颜色

python - 导入项目根以上的模块

python - 从前端创建 Django 调查表单

python - Django ORM 查询 - 如何从模型字段中减去时间?

redis - 如何使用redis存储帖子的查看次数和下载次数

python - 相关矩阵: Extract Variables with High R Values

python - 迭代可能是迭代器或单个元素的东西