简体   繁体   中英

Trouble with understanding how RabbitMQ can be used

i'm currently working on a Python web app that needs to implement RabbitMQ. The app is structured like that:

  1. The client connects to a HTTP server

  2. His connexion is send to a message queue that is connected to the main service of my app

  3. the main service receive the message and give the user his information

I understand how to make work RabbitMq using the documentation and tutorial on the website but I have trouble seeing how can it work with real tasks like displaying a web page or printing a file? How does my service connected to the message queue will read the message received and say: "oh, i'm gonna display this webpage".

Sorry if this is confusing, if you need further explanations on what i'm trying to get, just tell me.

Thanks for reading!

RabbitMq can be good to send message to service which can execute long running process - ie download big file, generate complex animation. Web server can't (or shoudn't) execute long running process.

Web page sends message to RabbitMq (ie. with parameters for long running process) and get unique number. When service has free worker then it checks if there is new message in queue, get it (with unique number) and start worker. When worker finish job then service send result to RabbitMQ with the same uniqe number.

At the same time web page uses JavaScript to run loop which periodically check in RabbitMQ if there is result with this unique number. If there is no result then it may display progressbar , if there is result then it may display this result.


Example: Celery - Distributed Task Queue .

Celery can use RabbitMQ to communicate with Django or Flask .
(but it can use other modules ie. Redis )

Using Celery with Django .

Flask - Celery Background Tasks


From Celery repo

Celery is usually used with a message broker to send and receive messages. 
The RabbitMQ, Redis transports are feature complete, but there's also 
experimental support for a myriad of other solutions, including using 
SQLite for local development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM