[英]Celery task hangs after calling .delay() in Django
從 django 應用程序調用導入任務的.delay()
方法時,進程卡住,請求永遠不會完成。
我們也沒有在控制台上收到任何錯誤。 使用 pdb 設置set_trace()
會產生同樣的結果。
審查了以下問題,但沒有幫助解決問題:
例如。:
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", RABBIT_URL)
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BROKER", RABBIT_URL)
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))
import time
from celery import shared_task
@shared_task
def upload_file(request_id):
time.sleep(request_id)
return True
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 設置 celery 應用程序。 我們需要確保在以下文件中導入並初始化了 celery 應用程序:
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 通過delay
或apply_async
調用可能會無限期地隨機掛起程序。 我嘗試了所有broker_transport_options
和retry_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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.