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

joh*_*ood 6 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.
Run Code Online (Sandbox Code Playgroud)

rh0*_*ium 12

我刚刚完成了这个,所以我可以对此有所了解.人们可能会想到所有这些伟大的文档,说明其中的一些内容会更加明显.

我假设您已经启动了RabbitMQ并且正在运行(它需要运行),并且您已经安装了dj-celery.

完成后,您需要做的就是在setup.py文件中包含这一行.

BROKER_URL = "amqp://guest:guest@localhost:5672//"
Run Code Online (Sandbox Code Playgroud)

然后你需要运行syncdb并使用以下命令启动它:

python manage.py celeryd -E -B --loglevel=info
Run Code Online (Sandbox Code Playgroud)

-E您要捕获的事件状态和-B状态你想celerybeats运行.前者使您能够在管理窗口中实际看到某些内容,后者允许您安排.最后,您需要确保实际上要捕获事件和状态.所以在另一个终端运行这个:

./manage.py celerycam
Run Code Online (Sandbox Code Playgroud)

然后最后你能够看到文档中提供的工作示例.. - 再次假设你创建了tasks.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
Run Code Online (Sandbox Code Playgroud)

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

Django任务经理

我希望这有帮助!!我还要补充一点对我有帮助的事情. 看RabbitMQ日志文件是关键,因为它帮助我确定django-celery实际上是在与RabbitMQ交谈.