You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中Celery delay()函数执行卡住无响应问题求助

Hey there, let's figure out why your Celery task is hanging when you call delay() in the Django shell. As a fellow Celery user who's been through similar headaches, here are practical troubleshooting steps to diagnose the issue:

1. Verify your message broker (Redis/RabbitMQ) is running and configured correctly

Celery relies entirely on a message broker to send and receive task instructions. If it can't connect to the broker, delay() will hang indefinitely waiting for a connection:

  • First, check your settings.py for the CELERY_BROKER_URL (or BROKER_URL for older Celery versions) — it should point to your running broker. For example, a local Redis instance would look like redis://localhost:6379/0.
  • Test the broker directly:
    • For Redis: Run redis-cli ping in your terminal. You should get a PONG response if it's running.
    • For RabbitMQ: Run rabbitmqctl status to confirm the service is active.
  • If the broker isn't running, start it first. If the URL is incorrect, fix it in your settings and restart any related processes.

2. Check if your Celery worker is up and running

While delay() should return immediately even if no worker is listening (the task just sits in the queue), a misconfigured worker can sometimes cause unexpected blocking. To start the worker correctly:

  • Navigate to your Django project root directory, then run:
    celery -A ns worker --loglevel=info
    
    (Replace ns with your project name, matching the ns.settings in your code.)
  • Watch the worker's output for errors — if it fails to connect to the broker or load tasks, that's a clue about the root problem.

3. Fix your Celery app initialization

Your shell code has a small but potentially problematic detail: app = Celery('') — the Celery constructor should take your project name as an argument (e.g., app = Celery('ns')). For better reliability, it's also best to initialize Celery in a dedicated file instead of the shell:

  • Create a celery.py file in your project root (next to settings.py):
    import os
    from celery import Celery
    
    # Set default Django settings module
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ns.settings')
    
    app = Celery('ns')
    # Load config from Django settings, using CELERY_* prefix
    app.config_from_object('django.conf:settings', namespace='CELERY')
    # Auto-discover tasks from all Django apps
    app.autodiscover_tasks()
    
  • Then add this to your project's __init__.py (the one in the ns folder):
    from .celery import app as celery_app
    
    __all__ = ('celery_app',)
    

This standard setup avoids manual configuration mistakes in the shell.

4. Debug step-by-step in the Django shell

Instead of running all code at once, execute each line individually to pinpoint where the hang happens:

  1. Run import os; os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ns.settings')
  2. Then from celery import Celery; app = Celery('ns'); app.config_from_object('django.conf:settings')
  3. Define your add task, then call add.delay(8,8)
    If the hang happens when creating the Celery app, it's almost certainly a broker connection issue. If it happens on delay(), the broker is likely unreachable or misconfigured.

5. Check for missing dependencies or environment mismatches

  • Confirm you're using the correct Python environment: Run import celery; print(celery.__version__) in the shell to ensure Celery is installed in the same environment as your Django project.
  • If you're using Redis, make sure the redis Python package is installed (pip install redis). For RabbitMQ, install amqp or librabbitmq.

内容的提问来源于stack exchange,提问作者Weiheng Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:02:24