【发布时间】: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 中
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