在Django中调用.delay()后,Celery任务挂起



从django应用程序调用导入任务的.delay()方法时,进程会陷入停滞,请求永远不会完成。

我们在控制台上也没有得到任何错误。用pdb设置set_trace()也会产生同样的结果。

审查了以下问题,但这些问题无助于解决问题:

调用芹菜任务因延迟和apply_async 而挂起

celerie.delay挂起(最近的,不是身份验证问题(

例如:

后端/设置.py

CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", RABBIT_URL)
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BROKER", RABBIT_URL)

backend/celey.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))

app/tasks.py

import time
from celery import shared_task
@shared_task
def upload_file(request_id):
time.sleep(request_id)
return True

app/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.")

问题出在Django中的芹菜应用程序的设置上。我们需要确保在以下文件中导入并初始化芹菜应用程序:

backend__init__.py

from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)

我遇到了这个问题,Celery通过delayapply_async调用的程序可能会随机无限期挂起。我尝试了所有的broker_transport_optionsretry_policy选项来让Celery恢复,但它仍然会发生。然后,我找到了这个解决方案,通过使用底层Python信号处理程序来强制执行块/函数的执行时间限制。

@contextmanager
def time_limit(seconds):
def signal_handler(signum, frame):
raise TimeoutException("Timed out!")
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
def my_function():
with time_limit(3):
celery_call.apply_sync(kwargs={"k1", "v1"}, expires=30)

相关内容

  • 没有找到相关文章

最新更新