python - 使用 Pika 客户端轮询 RabbitMQ 消息

标签 python rabbitmq pika

我想用 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/

相关文章:

python - 如何在 Pika 中同时运行消费者和发布者?

python - rabbitmq 使用 pika 线程

python - 无法从 scikit learn 导入模块

python - 使用 pandas 获取聚合行值的样本

python - 如何创建专门处理一个值的 matplotlib 颜色图?

python: nlp: 扩展英语缩写,比如 don't that's 等

python - Celery send_task 不发送任务

java - 使用剩余端点扩展或缩小 PCF 应用程序

node.js - NoSQL 架构白皮书

python - 当我尝试使用 pika (python) 向 RabbitMQ 确认消息时发生错误 "unknown delivery tag"