gpt4 book ai didi

push - ZeroMQ 推/拉模式

转载 作者:行者123 更新时间:2023-12-01 06:43:16 34 4
gpt4 key购买 nike

我决定编写一个测试代码来查看 pusher - many pullers bundle 是如何工作的,我的怀疑成真了。

拉取器按照连接的顺序接收消息,例如第一个消息由第一个连接的拉取器接收,第二个由第二个连接,等等。我模拟了其中一个拉取器在收到消息后保持忙碌的情况,但是当它是时候收到一条消息了,它无论如何都要排队,所以我“丢失”了消息。那很糟。我希望下一个“免费”puller 收到此消息。是真的吗?

我的测试代码。我使用 zmqpp 作为绑定(bind)

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 等被“遗漏”但实际上在 puller #1 中排队

最佳答案

是的,推/拉式套接字非常愚蠢,可以让这种情况发生。您可以使用其他套接字(例如路由器/经销商)将工作发送给免费工作人员。

关于push - ZeroMQ 推/拉模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30107055/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com