在拨打电话时.delay()
从 django 应用程序导入任务的方法,进程会卡住并且请求永远不会完成。
我们在控制台上也没有收到任何错误。
设置一个set_trace()
使用 pdb 会产生同样的结果。
审查了以下问题,但无助于解决问题:
调用 celery 任务因延迟而挂起并 apply_async
celery .delay 挂起(最近,不是身份验证问题)
Eg.:
后端/设置.py
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", RABBIT_URL)
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BROKER", RABBIT_URL)
后端/celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
app = Celery('backend')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
应用程序/任务.py
import time
from celery import shared_task
@shared_task
def upload_file(request_id):
time.sleep(request_id)
return True
应用程序/views.py
from rest_framework.views import APIView
from .tasks import upload_file
class UploadCreateAPIView(APIView):
# other methods...
def post(self, request, *args, **kwargs):
id = request.data.get("id", None)
# business logic ...
print("Going to submit task.")
import pdb; pdb.set_trace()
upload_file.delay(id) # <- this hangs the runserver as well as the set_trace()
print("Submitted task.")