简体   繁体   English

python rabbitMQ 消费者,它使用一条消息并调用作为后台进程运行的 API

[英]python rabbitMQ consumer which consumes a message and calls an API running as a background process

I have a simple use case where i want to consume messages from a RabbitMQ server, this messages are generated by third party application and whenever they generate any message i want my consumer to recieve it and make an API call.我有一个简单的用例,我想使用来自 RabbitMQ 服务器的消息,这些消息是由第三方应用程序生成的,每当它们生成任何消息时,我都希望我的消费者接收它并进行 API 调用。

I have got the below script to solve the problem but I am not sure if it is a production level solution, is there any way I can improve it or will it work fine if i run this script in background with the help of supervisor.我有以下脚本来解决问题,但我不确定它是否是生产级解决方案,有什么方法可以改进它,或者如果我在主管的帮助下在后台运行此脚本,它是否可以正常工作。

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', 
                                     pika.PlainCredentials("user", "pass")))
channel = connection.channel()

def callback(ch, method, properties, body):
    #Makes an API call

channel.basic_consume(queue="notifications.info", on_message_callback=callback, auto_ack=True)
channel.start_consuming()

Yes, that pretty much it.是的,差不多就是这样。 For production, you can implement multiprocessing and multiple consumers to scale out the solution.对于生产,您可以实施multiprocessing和多个消费者来扩展解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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