【发布时间】:2016-05-02 14:56:26
【问题描述】:
我决定编写一个测试代码来看看 pusher - many pullers bundle 的工作原理,我的猜想成真了。
Puller 按连接顺序接收消息,例如,第一个消息由连接的第一个 puller 接收,第二个由 2nd 等。我模拟了一种情况,其中一个 pullers 在收到消息后仍然很忙,但是当它是时候收到一条消息了,无论如何它都在排队,所以我已经“丢失”了消息。那很糟。我希望下一个“免费”拉取者能收到这条消息。这是真的吗?
我的测试代码。我使用 zmqpp 作为绑定
void main()
{
auto _socket = sIpcContext->CreateNewSocket(zmqpp::socket_type::push);
_socket->bind("tcp://*:4242");
for (auto i = 0; i < 3; ++i)
{
new std::thread([&](int _idx)
{
auto idx = _idx;
auto sock = sIpcContext->CreateNewSocket(zmqpp::socket_type::pull);
sock->connect("tcp://127.0.0.1:4242");
for (;;)
{
std::string msg;
sock->receive(msg);
std::cout << idx << " received: " << msg << std::endl;
if (idx == 1)
{
std::cout << "Puller 1 is now busy" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10000));
}
}
}, i);
}
for (auto i = 0;; ++i)
{
_socket->send(std::to_string(i));
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
我得到这个输出:
0 received: 0
0 received: 1
1 received: 2
Puller 1 is now busy
2 received: 3
0 received: 4
2 received: 6
0 received: 7
2 received: 9
0 received: 10
2 received: 12
0 received: 13
2 received: 15
如您所见,5、8 等“未命中”,但实际上在拉取器 #1 中排队
【问题讨论】: