我一直在尝试在本地计算机和远程服务器之间使用 zeromq 建立一个简单的“管道”。我在本地计算机上测试了脚本,它运行良好,但是当我尝试使用远程计算机作为终端(带有 SOCKET_PULL 的计算机)时,我开始遇到问题。任何进程发送的第一条消息总是丢失。
代码很简单,
推送(sender.php):
<?php
//Use the specified port or 5555 for sending jobs
$port = $_SERVER['argc'] > 1 ? $_SERVER['argv'][1] : '5555';
$jobN = $_SERVER['argc'] > 2 ? $_SERVER['argv'][2] : '';
$context = new ZMQContext();
// Socket to send messages on
$sender = new ZMQSocket($context, ZMQ::SOCKET_PUSH);
//If I want to connect to the server I use this line
$sender->connect("tcp://my-server-address.com:$port");
//If I want to connect to localhost I use this line
//$sender->connect("tcp://localhost:$port");
$sender->send('job-1' . ($jobN ? " $jobN" : ''));
$sender->send('job-2' . ($jobN ? " $jobN" : ''));
$sender->send('job-3' . ($jobN ? " $jobN" : ''));
$sender->send('job-4' . ($jobN ? " $jobN" : ''));
$sender->send('job-5' . ($jobN ? " $jobN" : ''));
$sender->send('job-6' . ($jobN ? " $jobN" : ''));
$sender->send('job-7' . ($jobN ? " $jobN" : ''));
$sender->send('job-8' . ($jobN ? " $jobN" : ''));
$sender->send('job-9' . ($jobN ? " $jobN" : ''));
$sender->send('job-10' . ($jobN ? " $jobN" : ''));
echo 'done';
接收器(worker.php)
<?php
//Use the specified port or 5556 for getting finished jobs
$port = $_SERVER['argc'] > 1 ? $_SERVER['argv'][1] : '5555';
// Prepare our context and socket
$context = new ZMQContext();
$receiver = new ZMQSocket($context, ZMQ::SOCKET_PULL);
$receiver->bind("tcp://*:$port");
$count = 1;
while(true) {
$string = $receiver->recv();
echo "Received $string $count\n";
$count += 1;
}
如果我在本地运行代码(一个终端中的“php worker.php”和另一个终端中的“php sender.php”)。
我明白了
Received job-1 1
Received job-2 2
Received job-3 3
Received job-4 4
Received job-5 5
Received job-6 6
Received job-7 7
Received job-8 8
Received job-9 9
Received job-10 10
但是如果我使用遥控器(本地 mu 中的发送者和遥控器中的接收者),我会得到
Received job-2 1
Received job-3 2
Received job-4 3
Received job-5 4
Received job-6 5
Received job-7 6
Received job-8 7
Received job-9 8
Received job-10 9
难道我做错了什么??
注意:我不能锁定发送者(并且 SOCKET_REQ 锁定执行等待回复)。无论如何,0mq 应该在没有锁的情况下工作。注意:我的本地是一台 mac 计算机,服务器是一个带有 ubuntu 的亚马逊实例(我认为这不会影响,但我正在写它以防万一)。
编辑:澄清一下,我不想通过互联网发送数据,我只是想在遇到这个问题时以高延迟测试 zeromq。