我想用 Python 创建一个 RabbitMQ 接收器/消费者,但不知道如何检查消息。我试图在自己的循环中执行此操作,而不是使用 pika 中的回调。
如果我理解的话,在 Java 客户端中我可以使用 getBasic()
来检查是否有任何可用的消息而不阻塞。我不介意在收到消息时阻塞,但我不想在收到消息之前阻塞。
我没有找到任何明确的例子,也没有弄清楚pika中相应的调用。
最佳答案
如果你想同步执行,那么你需要查看鼠兔BlockingConnection
The BlockingConnection creates a layer on top of Pika’s asynchronous core providng methods that will block until their expected response has returned. Due to the asynchronous nature of the Basic.Deliver and Basic.Return calls from RabbitMQ to your application, you are still required to implement continuation-passing style asynchronous methods if you’d like to receive messages from RabbitMQ using basic_consume or if you want to be notified of a delivery failure when using basic_publish.
此处有更多信息和示例
https://pika.readthedocs.org/en/0.9.12/connecting.html#blockingconnection
关于python - 使用 Pika 客户端轮询 RabbitMQ 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15138976/