问题描述
我一直在试图弄清楚在使用 pika 时应该使用哪种连接形式,据我所知,我有两种选择.
I've been trying to figure out which form of connection i should use when using pika, I've got two alternatives as far as I understand.
BlockingConnection
或 SelectConnection
,但我不太确定这两者之间的区别(即 BlockingConnection 阻塞是什么?等等)
Either the BlockingConnection
or the SelectConnection
, however I'm not really sure about the differences between these two (i.e. what is the BlockingConnection blocking? and more)
pika
的文档说 SelectConnection
是连接到 rabbit 的首选方式,因为它提供了多种事件通知方法,包括 select、epoll、kqueue 和 poll".
The documentation for pika
says that SelectConnection
is the preferred way to connect to rabbit since it provides "multiple event notification methods including select, epoll, kqueue and poll."
所以我想知道这两种不同类型的连接的含义是什么?
So I'm wondering what are the implications of these two different kinds of connections?
PS:我知道我不应该在标题中添加标签,但在这种情况下,我认为它确实有助于澄清问题.
PS: I know I shouldn't put a tag in the title but in this case I think it does help to clarify the question.
推荐答案
如果您的应用程序架构可以从异步设计中受益,则 SelectConnection 很有用,例如在 RabbitMQ IO 完成时做其他事情(例如切换到其他 IO 等).这种类型的连接使用回调来指示函数何时返回.例如,您可以为
The SelectConnection is useful if your application architecture can benefit from an asynchronous design, e.g. doing something else while the RabbitMQ IO completes (e.g. switch to some other IO etc) . This type of connection uses callbacks to indicate when functions return. For example you can declare callbacks for
on_connected、on_channel_open、on_exchange_declared、on_queue_declared 等
on_connected, on_channel_open, on_exchange_declared, on_queue_declared etc.
...在这些事件被触发时执行操作.
...to perform operations when these events are triggered.
如果您的 RabbitMQ 服务器(或与该服务器的连接)速度较慢或过载,则该好处尤其明显.
The benefit is especially good if your RabbitMQ server (or connection to that server) is slow or overloaded.
BlockingConnection 就是这样 - 它阻塞直到被调用的函数返回.因此它将阻塞执行线程,直到连接或 channel_open 或 exchange_declared 或 queue_declared 返回例如.也就是说,编写这种序列化逻辑通常比异步 SelectConnection 逻辑更简单.对于具有响应式 RabbitMQ 服务器的简单应用程序,这些在 IMO 上也可以正常工作.
BlockingConnection on the hand is just that - it blocks until the called function returns. so it will block the execution thread until connected or channel_open or exchange_declared or queue_declared return for example. That said, its often simpler to program this sort of serialized logic than the async SelectConnection logic. For simple apps with responsive RabbitMQ servers these also work OK IMO.
我想你已经阅读了 Pika 文档 http://pika.readthedocs.io/en/stable/intro.html,如果没有,那么在你使用 Pika 之前这是绝对重要的信息!
I suppose you've read the Pika documentation already http://pika.readthedocs.io/en/stable/intro.html, if not, then this is absolutely vital information before you use Pika!
干杯!
这篇关于pika 使用哪种连接形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!