I have a Django project where celery is used to perform asynchronous tasks, broker is using rabbitmq, and I run everything right on my MacBook and then on a CentOS virtual machine, but whenWhen I deploy this project with docker, if I request a view that calls a celery task’s Daley mode, it will be blocked.
I created a test project on my GitHub: https://github.com/fengyoucha…
@shared_task def add(x, y): return x + y
In fact, other tasks are defined, but in the view of this project, only the task is called.
def index(request): # Mapping /A = int (request.GET.get ('a', 1))B = int (request.GET.get ('b', 2))Add.delay (a, b)Return HttpResponse ("Hello world")Def hello (request): /hello mappingReturn HttpResponse ("he")LLO ")
This project uses docker-compose deployment.
docker-compose.ymlThree services are defined.
- rabbitmq – rabbitmq service
- web – implement
python mananage.py runserver 0.0.0.0:8000
- celery – implement
celery -A proj worker -l info
docker-compose upAfter launching service
Testing web services
This command will be blocked in the index method.
add.delay(a, b)There was a blockage.
Returns the content “hello”, and the Hello method does not call task.
In the physical machine, start the proj project, of course, just start web, that is, run.
python manage.py runserver
To prove that only running the web on a docker will block, other environments are required to use the services defined by docker-compose.yml
Keep the startup celery and rabbit docker containers (and you don’t have to close the web container in the docker, but you don’t actually need to do anything…
I have defined in docker-compose.yml that rabbit’s 5672 port is mapped to the physical machine’s 5672 port, and in proj, the Django project, I read the environment variables
RABBITMQ_HOSTTo set up rabbitmq server that needs to be linked
projConfigure broker related code in
RABBITMQ_HOST = os.environ.get("RABBITMQ_HOST", 'rabbitmq') CELERY_BROKER_URL = 'amqp://guest:guest@%s//' % RABBITMQ_HOST
The environment variables are changed under the physical machine, and a new web is created.
export RABBITMQ_HOST=localhost python manage.py runserver 0.0.0.0:18000
Access the view of the web
The request normally returns “Hello world”, and the celery container also prints out the execution.
My other tests
I’ve also done other tests, such as starting the web in docker, using rabbit in the physical machine, celery in the physical machine, as well as blocking.
Anyway, just run web in docker and execute task.
delayMethod will block.
This problem has been troubling for a long time. Dockers deploy Ceery’s Django project. Anyone with successful experience, or where I have configuration problems, please advise.
No answer has been answered so far.