rabbitmq - 如何为django-celery设置后端。我设置了CELERY_RESULT_BACKEND,但无法识别

标签 rabbitmq celery django-celery

我在celeryconfig.py中设置了CELERY_RESULT_BACKEND =“amqp”
但我得到:

>>> from tasks import add
>>> result = add.delay(3,5)
>>> result.ready()

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/djangoprojects/venv/local/lib/python2.7/site-packages/celery/result.py", line 105, in ready
    return self.state in self.backend.READY_STATES
  File "/djangoprojects/venv/local/lib/python2.7/site-packages/celery/result.py", line 184, in state
    return self.backend.get_status(self.task_id)
  File "/djangoprojects/venv/local/lib/python2.7/site-packages/celery/backends/base.py", line 414, in _is_disabled
    raise NotImplementedError("No result backend configured.  "
NotImplementedError: No result backend configured.  Please see the documentation for more information.

最佳答案

我刚刚经历了这个,所以我可以对此有所了解。有人可能会认为,对于所有出色的文档而言,其中某些内容将变得更加明显。

我假设您同时启用了RabbitMQ并正常运行(它需要运行),并且已经安装了dj-celery

一旦有了这些,您要做的就是将这一行包括在setting.py文件中。

BROKER_URL = "amqp://guest:guest@localhost:5672//"

然后,您需要运行syncdb并使用以下命令启动该程序:
python manage.py celeryd -E -B --loglevel=info
-E声明要捕获事件,而-B声明要运行celerybeats。前者使您能够在管理窗口中实际看到某些内容,而后者则使您能够进行计划。最后,您需要确保确实要捕获事件和状态。因此,在另一个终端中运行以下命令:
./manage.py celerycam

然后,您终于可以看到文档中提供的工作示例了。-再次假设您创建了task.py。
>>> result = add.delay(4, 4)
>>> result.ready() # returns True if the task has finished processing.
False
>>> result.result # task is not ready, so no return value yet.
None
>>> result.get()   # Waits until the task is done and returns the retval.
8
>>> result.result # direct access to result, doesn't re-raise errors.
8
>>> result.successful() # returns True if the task didn't end in failure.
True

此外,您还可以在管理面板中查看自己的状态。

我希望这有帮助!!我还要再添加一件事,这对我有帮助。观看RabbitMQ日志文件是关键,因为它有助于我识别django-celery实际上正在与RabbitMQ对话。

关于rabbitmq - 如何为django-celery设置后端。我设置了CELERY_RESULT_BACKEND,但无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10660202/

相关文章:

python - Worker 启动,然后关闭。没有错误

python - 优先考虑 celery 队列/任务

networking - 在 ec2 中使用两个可用区会引入网络分区吗?

python - Celeryd 运行多个守护进程

python - 在 Django 应用程序的 Celery 任务中使用事务会导致问题吗?

RabbitMQ 安全设计,用于从服务器声明队列(并从客户端使用)

java - 没有密码的 RabbitMQ 身份验证

node.js - 多个队列在一个 channel 中消费

spring - 使用 RabbitMQ 插件在 Grails 中创建队列运行时

python - Celery multi 以 1 个或多个进程启动