我在ZeroMQ遇到了一个奇怪的问题,其中一些消息被卡住了,当新消息到达时就会失败。这就像新消息将卡住的消息推到了门上(可怕的比较,我知道)。
我的代码非常简单:
rep.php
$context = new ZMQContext;
$receiver = new ZMQSocket($context, ZMQ::SOCKET_PULL);
$receiver->connect("tcp://localhost:8022");
$receiver2 = new ZMQSocket($context, ZMQ::SOCKET_PULL);
$receiver2->connect("tcp://localhost:8024");
for (;;) {
echo $receiver->recv() . PHP_EOL;
echo $receiver2->recv() . PHP_EOL;
}
cnt.php和cnt2.php(相同的代码,不同的端口)
$context = new ZMQContext;
$work = new ZMQSocket($context, ZMQ::SOCKET_PUSH);
$work->bind('tcp://*:8022');
$work->send('Hello World');
cnt.php发送到8022,cnt2.php发送到8024.它们不时被执行并向rep.php发送消息。但是,有些消息会被卡住。如果我从cnt.php发送了4条消息,则没有收到任何消息,但是当我从cnt2.php发送1时,我一次收到5条消息。有什么想法吗?
答案 0 :(得分:1)
我不是PHP专家,而是猜测语法和功能。如果我错了,请纠正我
echo $receiver->recv() . ' - ' . $receiver2->recv();
recv()应该是一个阻止调用。
$receiver->recv()
阻止,直到收到一些消息。echo
不会立即回复此消息$receiver2->recv()
$receiver2->recv()
由于您希望独立处理套接字recv(),您应该使用轮询或基于事件的异步I / O.
[尝试解决方案]
$poll = new ZMQPoll();
$poll->add($receiver, ZMQ::POLL_IN);
$poll->add($receiver2, ZMQ::POLL_IN);
$readable = $writeable = array();
while(true) {
$events = $poll->poll($readable, $writeable);
foreach($readable as $socket) {
$message = $socket->recv();
echo $message, PHP_EOL;
}
}