Article From:https://segmentfault.com/q/1010000011710218
Question:

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…

Task

@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.

View

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 – implementpython mananage.py runserver 0.0.0.0:8000
  • celery – implementcelery -A proj worker -l info

Usedocker-compose upAfter launching service

Testing web services

Request index

curl localhost:8000

This command will be blocked in the index method.add.delay(a, b)There was a blockage.

Request helle

curl localhost:8000/hello

Returns the content “hello”, and the Hello method does not call task.delayMethod

Further testing

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 variablesRABBITMQ_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

curl locahost:18000

The request normally returns “Hello world”, and the celery container also prints out the execution.addTask log.

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.

Last

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.

Answer 0:

No answer has been answered so far.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *