关于process的消息队列中的问提

<?php
$workers = [];
$worker_num = 2;

for($i = 0; $i < $worker_num; $i++)
{
    $process = new swoole_process('callback_function', false, false);
    $process->useQueue();
    $pid = $process->start();
    $workers[$pid] = $process;
    //echo "Master: new worker, PID=".$pid."\n";
}

function callback_function(swoole_process $worker)
{
    //echo "Worker: start. PID=".$worker->pid."\n";
    //recv data from master
    $recv = $worker->pop();
    echo "From Master: $recv\n";
    $worker->push(" \n   hehe   \n ");

    sleep(2);
    $worker->exit(0);

}

foreach($workers as $pid => $process)
{
    $process->push("hello worker[$pid]\n");
    $result = $process->pop();
    echo "From worker: $result\n";
}

for($i = 0; $i < $worker_num; $i++)
{
    $ret = swoole_process::wait();
    $pid = $ret['pid'];
    unset($workers[$pid]);
    echo "Worker Exit, PID=".$pid.PHP_EOL;
}

这上面的代码我是根据官网的demo稍微改了一下, 运行结果如下

From Master: hello worker[3161]

From worker:  
   hehe   

From Master: hello worker[3162]

From worker:  
   hehe   

Worker Exit, PID=3161
Worker Exit, PID=3162

这个一切正常,但是当 仅仅去掉了 sleep

From Master: hello worker[3190]

From worker:  
   hehe   

PHP Warning:  swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /home/sun/learn/swoole/process/demo.php on line 29
PHP Warning:  swoole_process::pop(): msgrcv() failed. Error: Invalid argument[22] in /home/sun/learn/swoole/process/demo.php on line 30
From worker: 
PHP Warning:  swoole_process::pop(): msgrcv() failed. Error: Identifier removed[43] in /home/sun/learn/swoole/process/demo.php on line 18
From Master: 
PHP Warning:  swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /home/sun/learn/swoole/process/demo.php on line 20
Worker Exit, PID=3191
Worker Exit, PID=3190

我不理解,请问这个代码的运行流程没问题啊,为什么push会失败呢? 或者请给我一个使用消息队列在swoole_process中通讯的例子(swoole_src里的是单向的,没有子进程向主进程发送)

2015-08-10 22:11
0
0

韩天峰 - 关注外星人

赞同来自:

这是一个已知BUG,子进程在退出时消息队列被销毁了,所以会报此错误。请使用最新的1.7.19-rc2版本。

2015-08-27 12:20
0

请叫我特靠谱

赞同来自:

function callback_function(swoole_process $worker) { //echo "Worker: start. PID=".$worker->pid."\n"; //recv data from master $recv = $worker->pop(); echo "From Master: $recv\n"; $worker->push(" \n hehe \n ");

sleep(2);
$worker-&gt;exit(1);

}

在子进程中 $work->exit(1) 消息队列是不会被销毁的。

2018-05-15 11:30

要回复问题请先登录注册