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

Docker环境下Django集成Celery时task.get()无法正常工作求助

Django + Celery in Docker: Task.get() Not Working (Even Though RabbitMQ Receives Messages)

Hey there! I’ve run into this exact same headache when setting up Celery with Django in Docker, so I totally get how frustrating this is. Let’s walk through the most common fixes for this scenario—where messages land in RabbitMQ but task.get() hangs or fails to return a result.

1. Your Celery Worker Isn’t Running (or Connected Properly)

Even if RabbitMQ gets the task message, if your Celery worker isn’t up and listening, the task will never execute. Let’s verify:

  • First, check if your Celery worker container is running:
    docker ps
    
  • If it’s running, pull the logs to confirm it connected to RabbitMQ successfully:
    docker logs <your-celery-worker-container-name>
    
    Look for lines like Connected to amqp://user:**@rabbitmq:5672//—if you don’t see this, your worker can’t reach the RabbitMQ service.
  • Double-check your Celery broker URL matches your Docker setup. In your celery.py or worker command, it should reference the RabbitMQ service name from your docker-compose.yml:
    app = Celery('myproject', broker='amqp://user:password@rabbitmq:5672//')
    

2. Mismatched Task Serialization Settings

Sometimes the task is sent, but the worker can’t deserialize it because Django and Celery use different serialization formats.

  • Add these settings to your Django settings.py to standardize on JSON:
    CELERY_ACCEPT_CONTENT = ['json']
    CELERY_TASK_SERIALIZER = 'json'
    CELERY_RESULT_SERIALIZER = 'json'
    
  • Make sure your Celery worker is pulling these settings—if you start it with celery -A myproject worker -l info, it should inherit them automatically if your Celery app is linked to Django correctly.

3. Missing or Unreachable Result Backend

When you call task.get(), Celery needs to fetch the task result from a backend. If you haven’t configured this, or the worker can’t reach it, get() will hang.

  • Add a result backend (Redis is a reliable choice for Docker setups) to settings.py:
    CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
    
    Again, redis should match the service name in your docker-compose.yml.
  • Verify the Redis container is running and check worker logs for any connection errors to the backend.

4. Docker Network Isolation

Docker containers run in isolated networks by default. Ensure all your services (Django, Celery, RabbitMQ, Redis) are on the same network.

  • In your docker-compose.yml, group all services under a shared network:
    version: '3'
    services:
      web:
        build: .
        networks:
          - celery-network
      celery:
        build: .
        command: celery -A myproject worker -l info
        networks:
          - celery-network
      rabbitmq:
        image: rabbitmq:3-management
        networks:
          - celery-network
      redis:
        image: redis:alpine
        networks:
          - celery-network
    networks:
      celery-network:
        driver: bridge
    

5. Code Sync or Permission Issues (Local Dev)

If you’re using volume mounts for your code, the Celery worker might not pick up the latest task changes.

  • Restart the Celery worker container after editing tasks:
    docker-compose restart celery
    
  • Ensure the user inside the Celery container has permission to read your task files—adjust your Dockerfile’s USER setting or volume permissions if needed.

If you’re getting a specific error when calling task.get(), drop that detail and we can narrow it down further!

内容的提问来源于stack exchange,提问作者Ryan Skene

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:11:23