获取 运行 Celery 任务的结果(Windows 10 32 位)

get results of running a task on Celery (Windows 10 32 bits)

从早上开始,我已经解决这个问题大约 10 个小时,但找不到任何解决方案。

我正在学习使用 Celery。我想在我的一个 Django 项目中使用它。我按照官方教程操作,但我无法取回我的任务结果。

无论是否配置了后端,我在尝试访问结果时总是出现此错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\result.py", line 223, in get
    return self.backend.wait_for_pending(
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\backends\base.py", line 698, in wait_for_pending
    meta = self.wait_for(
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\backends\base.py", line 1029, in _is_disabled
    raise NotImplementedError(E_NO_BACKEND.strip())
NotImplementedError: No result backend is configured.
Please see the documentation for more information.

这是我的文件夹结构:

|- task_exe
   |- celery.py
   |- settings.py
   |- ...
|- tadder
   |- tasks.py
   |- ...
|- env
|- manage.py
|- ...

这是我的 celery.py 文件:

from __future__ import absolute_import, unicode_literals
from celery import Celery
from tadder.tasks import add

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'task_exe.settings')
app = Celery("task_exe", backend='rpc://' , broker='amqp://localhost')
app.config_from_object('django.conf:settings', namespace="CELERY")

app.autodiscover_tasks()

这是我的 tasks.py 文件:

from __future__ import absolute_import, unicode_literals
from celery import shared_task

@shared_task
def add(a, b): return a + b

这是我用来启动 celery worker 的命令:(使用 windows 10 32 位)

path/to/my_django_project_where_you_can_find_manage.py> celery -A task_exe workter -l info --pool=solo

当我注册一个任务时,它被 celery 接收并执行,我可以在输出控制台中看到结果。但是,当我尝试使用 result.get() 获取结果时,出现了上述错误。

我也尝试过更改后端。我将后端更改为“db+sqlite://result.sqlite3”,并在启动 celery worker 时创建了一个 result.sqlite3 文件。我还在生成的 .sqlite3 文件中找到了任务的 ID,但它没有改变任何内容。我仍然收到错误消息。

这是我用来注册任务和检索结果的命令(来自 django 的交互式 shell (manage.py shell) 的输出):

>>> from tadder.tasks import add  
>>> r = add.delay(10, 20)
>>> r.status
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\result.py", line 477, in state
    return self._get_task_meta()['status']
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\result.py", line 416, in _get_task_meta  
    return self._maybe_set_cache(self.backend.get_task_meta(self.id))
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\backends\base.py", line 558, in get_task_meta
    meta = self._get_task_meta_for(task_id)
AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for'
>>> r.get()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\result.py", line 223, in get
    return self.backend.wait_for_pending(
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\backends\base.py", line 698, in wait_for_pending
    meta = self.wait_for(
  File "G:\progs\python\Django-projects\celery_tutorial\env\lib\site-packages\celery\backends\base.py", line 1029, in _is_disabled
    raise NotImplementedError(E_NO_BACKEND.strip())
NotImplementedError: No result backend is configured.
Please see the documentation for more information.
>>>

这是芹菜工人控制台的输出:

-------------- celery@DESKTOP-N6RJF73 v5.1.1 (sun-harmonics)   
--- ***** -----
-- ******* ---- Windows-10-10.0.19041-SP0 2021-06-20 17:18:15   
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         task_exe:0x35c2778
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     rpc://
- *** --- * --- .> concurrency: 2 (solo)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . tadder.tasks.add

[2021-06-20 17:18:15,954: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2021-06-20 17:18:15,990: INFO/MainProcess] mingle: searching for neighbors
[2021-06-20 17:18:17,044: INFO/MainProcess] mingle: all alone
[2021-06-20 17:18:17,101: WARNING/MainProcess] g:\progs\python\django-projects\celery_tutorial\env\lib\site-packages\celery\fixups\django.py:20xups\django.py:203: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  warnings.warn('''Using settings.DEBUG leads to a memory

[2021-06-20 17:18:17,102: INFO/MainProcess] celery@DESKTOP-N6RJF73 ready.
[2021-06-20 17:19:35,297: INFO/MainProcess] Task tadder.tasks.add[77f22b77-e7ab-4a67-b12c-b8ebc282b629] received
[2021-06-20 17:19:40,170: INFO/MainProcess] Task tadder.tasks.add[77f22b77-e7ab-4a67-b12c-b8ebc282b629] succeeded in 0.0s: 30 
[2021-06-20 17:32:16,356: INFO/MainProcess] Task tadder.tasks.add[cf8351d7-c8df-47e1-826d-26f6dcb007ac] received
[2021-06-20 17:32:16,358: INFO/MainProcess] Task tadder.tasks.add[cf8351d7-c8df-47e1-826d-26f6dcb007ac] succeeded in 0.0s: 30

任何人都可以帮助我让这个狗屎工作吗?!


我总是在 Windows 中遇到 Celery 的问题,尽管从技术上讲你 可以 运行 它,通过使用 --pool=solo 您已经指出的命令。该文档在这方面有点误导,我总是得到不一致的功能。我最终获得了 Windows、ubuntu 20 的 WSL,并且我 运行 redis(或 rabbitmq)一整天都有进程和线程,没有任何问题。此外,它更能模拟生产环境,因为您很可能 运行 不会只有一个工人。

这是我用于测试的示例配置

app = Celery ('app', broker="redis://", backend="redis://", include=['app.tasks'])

显然在 4.0 中删除了支持,但在 4.3 中重新添加了支持。我确定有人拥有 windows 10 台机器,运行ning celery on windows 我可以插话。