【问题标题】:celery app.delay() is keep on waiting for the response, not workingcelery app.delay() 一直在等待响应,不工作
【发布时间】:2021-11-05 10:51:36
【问题描述】:

我正在尝试在我的 Django 应用程序中使用 Redis 代理来试验 celery,但我很难让它写出来,谁能指出我遗漏的地方

我的 init.py

from __future__ import absolute_import
from .celery import app as celery_app

__all__ = ('celery_app', )

我的 base_settings.py

CELERY_BROKER_URL = 'redis://localhost:6379/'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_ALWAYS_EAGER = True

我的 celery.py

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
app = Celery('myapp')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

app.conf.enable_utc = False
app.conf.update(timezone='Asia/Kolkata', task_always_eager=True)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

在我看来.py

@app.task(name="test celery")
def add(a, b):
    print(a, b, a + b)


@api_view(['GET'])
def testcelery(request):
    a = request.GET.get('a', 0)
    b = request.GET.get('b', 0)
    add.delay(a, b)
    return Response({"status": "ok"}, status=status.HTTP_200_OK)

我的芹菜工人的输出

>celery -A lylo worker --loglevel=info -c 5
 -------------- celery@LAPTOP-VQHQANA1 v5.1.1 (sun-harmonics)
--- ***** -----
-- ******* ---- Windows-10-10.0.19041-SP0 2021-09-08 22:00:46
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         lylo:0x289e11010b8
- ** ---------- .> transport:   redis://localhost:6379//
- ** ---------- .> results:     redis://localhost:6379/
- *** --- * --- .> concurrency: 5 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . myapp.celery.debug_task
  . test celery

[2021-09-08 22:00:47,585: INFO/SpawnPoolWorker-4] child process 6672 calling self.run()
[2021-09-08 22:00:47,585: INFO/SpawnPoolWorker-2] child process 33680 calling self.run()
[2021-09-08 22:00:47,601: INFO/SpawnPoolWorker-3] child process 36712 calling self.run()
[2021-09-08 22:00:47,629: INFO/SpawnPoolWorker-5] child process 21636 calling self.run()
[2021-09-08 22:00:47,689: INFO/SpawnPoolWorker-1] child process 27176 calling self.run()
[2021-09-08 22:00:49,002: INFO/MainProcess] Connected to redis://localhost:6379//
[2021-09-08 22:00:51,021: INFO/MainProcess] mingle: searching for neighbors
[2021-09-08 22:00:58,046: INFO/MainProcess] mingle: all alone
[2021-09-08 22:01:08,068: WARNING/MainProcess] d:\installed\anaconda3\lib\site-packages\celery\fixups\django.py:204: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  leak, never use this setting in production environments!''')

[2021-09-08 22:01:08,071: INFO/MainProcess] celery@LAPTOP-VQHQANA1 ready.

我的 redis-cli 监视器

在我的 redis-cli 中

127.0.0.1:6379> keys *
1) "_kombu.binding.reply.celery.pidbox"
2) "_kombu.binding.celery"
3) "_kombu.binding.celery.pidbox"
4) "_kombu.binding.celeryev"

但是当我发送请求时,它没有返回任何输出,而是继续等待响应

【问题讨论】:

    标签: django redis celery django-celery


    【解决方案1】:

    您设置了CELERY_ALWAYS_EAGER = True,这使得 celery 任务在本地执行而不是发送到队列。见https://docs.celeryproject.org/en/stable/userguide/configuration.html#std-setting-task_always_eager

    请求的响应为空,因为您在视图中隐式返回 None

    要将任务发送到队列,请设置CELERY_ALWAYS_EAGER = False 或将其删除(默认值为False)。

    【讨论】:

    • 1.请求的响应是空的,因为您在视图上隐式返回 None。:我的意思是,它正在等待响应,请注意我添加了 "return Response({"status": "ok"}, status=status. HTTP_200_OK)" 它仍在等待响应 2. 设置 CELERY_ALWAYS_EAGER = False:这样做但仍然相同
    • @krishna 你能访问任何其他端点吗?你的 django 服务器上有日志吗?您一直在等待响应似乎与芹菜无关。
    • 如果我从这一行删除 .delay() add.delay(a, b) 它工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多