首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
关于useQueue的问题
我在学习swoole的进程之间的通讯 {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->exit(0); } }}} 我的目的就是主进程使用消息队列向每一个子进程发送信息,之后子进程将信息输出 代码的运行结果是 {{{ From Master: hi 4863 worker , i am master ; PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 16 From Master: }}} 我感到很好奇,好像没写错什么啊?为什么第一个进程正常,第二个就出现了错误呢? 之后在我原先的代码上,添加了这么一段 {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->exit(0); } //上面的没变,只是多了下面这段 for($i = 0; $i < $numbers; $i++) { $ret = swoole_process::wait(); $pid = $ret['pid']; unset($workers[$pid]); echo "Worker Exit, PID=".$pid.PHP_EOL; } }}} 这样一来,输出结果是 {{{ From Master: hi 4876 worker , i am master ; From Master: hi 4877 worker , i am master ; Worker Exit, PID=4876 Worker Exit, PID=4877 }}} 看着输出变好了,是我预想的那样, 但是 swoole_process::wait() 是 回收结束运行的子进程 。 这个和我的进程通讯,消息队列有什么关系呢? 我又接着测试 {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); $recv = $worker->pop();//父进程接收 echo PHP_EOL."From worker: ".$recv.PHP_EOL; } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->push(" h1 master , i am ".$worker->pid);//子进程向父进程发送 $worker->exit(0); } for($i = 0; $i < $numbers; $i++) { $ret = swoole_process::wait(); $pid = $ret['pid']; unset($workers[$pid]); echo "Worker Exit, PID=".$pid.PHP_EOL; } }}} 这个代码的意思是主进程和子进程的数据通信 输出结果 {{{ From Master: hi 4803 worker , i am master ; From worker: h1 master , i am 4803 PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Identifier removed[43] in /var/www/learn/SWOOLE/process/demo.php on line 13 From worker: From Master: hi 4804 worker , i am master ; PHP Warning: swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 20 Worker Exit, PID=4804 Worker Exit, PID=4803 }}} 这点我就不理解了,我尝试去掉最后的swoole_process::wait(); {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); $recv = $worker->pop();//父进程接收 echo PHP_EOL."From worker: ".$recv.PHP_EOL; } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->push(" h1 master , i am ".$worker->pid);//子进程向父进程发送 $worker->exit(0); } //下面的删除了 }}} 结果是 {{{ From Master: hi 4934 worker , i am master ; From worker: h1 master , i am 4934 PHP Warning: swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 12 PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 13 From worker: PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Identifier removed[43] in /var/www/learn/SWOOLE/process/demo.php on line 18 From Master: PHP Warning: swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 20 }}} 我不明白为什么运行结果这样, 难道消息队列的通讯,主进程和所有子进程共用一个消息队列吗?(和管道一个子进程一个管道不同?) 难道消息队列的通讯,必须使用 swoole_process::wait(); 全部的子进程才能收到主进程的信息?否则就只能和第一个进程通讯? pleas help me !!
发布于8年前 · 0 次浏览 · 来自
提问
文
文刀无尽Nick
我在学习swoole的进程之间的通讯 {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->exit(0); } }}} 我的目的就是主进程使用消息队列向每一个子进程发送信息,之后子进程将信息输出 代码的运行结果是 {{{ From Master: hi 4863 worker , i am master ; PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 16 From Master: }}} 我感到很好奇,好像没写错什么啊?为什么第一个进程正常,第二个就出现了错误呢? 之后在我原先的代码上,添加了这么一段 {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->exit(0); } //上面的没变,只是多了下面这段 for($i = 0; $i < $numbers; $i++) { $ret = swoole_process::wait(); $pid = $ret['pid']; unset($workers[$pid]); echo "Worker Exit, PID=".$pid.PHP_EOL; } }}} 这样一来,输出结果是 {{{ From Master: hi 4876 worker , i am master ; From Master: hi 4877 worker , i am master ; Worker Exit, PID=4876 Worker Exit, PID=4877 }}} 看着输出变好了,是我预想的那样, 但是 swoole_process::wait() 是 回收结束运行的子进程 。 这个和我的进程通讯,消息队列有什么关系呢? 我又接着测试 {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); $recv = $worker->pop();//父进程接收 echo PHP_EOL."From worker: ".$recv.PHP_EOL; } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->push(" h1 master , i am ".$worker->pid);//子进程向父进程发送 $worker->exit(0); } for($i = 0; $i < $numbers; $i++) { $ret = swoole_process::wait(); $pid = $ret['pid']; unset($workers[$pid]); echo "Worker Exit, PID=".$pid.PHP_EOL; } }}} 这个代码的意思是主进程和子进程的数据通信 输出结果 {{{ From Master: hi 4803 worker , i am master ; From worker: h1 master , i am 4803 PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Identifier removed[43] in /var/www/learn/SWOOLE/process/demo.php on line 13 From worker: From Master: hi 4804 worker , i am master ; PHP Warning: swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 20 Worker Exit, PID=4804 Worker Exit, PID=4803 }}} 这点我就不理解了,我尝试去掉最后的swoole_process::wait(); {{{ <?php $workers = array(); $numbers = 2; for($i=1;$i<=$numbers ; $i++){ $process = new swoole_process('callback' , false ,false ); $process->useQueue(); $pid = $process->start(); $workers[$pid]=$process; } foreach($workers as $pid =>$worker){ $worker->push(" hi $pid worker , i am master ; "); $recv = $worker->pop();//父进程接收 echo PHP_EOL."From worker: ".$recv.PHP_EOL; } function callback( swoole_process $worker ){//子进程 $recv = $worker->pop();//如果队列中没有数据,pop()方法会阻塞等待 echo PHP_EOL."From Master: ".$recv.PHP_EOL; $worker->push(" h1 master , i am ".$worker->pid);//子进程向父进程发送 $worker->exit(0); } //下面的删除了 }}} 结果是 {{{ From Master: hi 4934 worker , i am master ; From worker: h1 master , i am 4934 PHP Warning: swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 12 PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 13 From worker: PHP Warning: swoole_process::pop(): msgrcv() failed. Error: Identifier removed[43] in /var/www/learn/SWOOLE/process/demo.php on line 18 From Master: PHP Warning: swoole_process::push(): msgsnd() failed. Error: Invalid argument[22] in /var/www/learn/SWOOLE/process/demo.php on line 20 }}} 我不明白为什么运行结果这样, 难道消息队列的通讯,主进程和所有子进程共用一个消息队列吗?(和管道一个子进程一个管道不同?) 难道消息队列的通讯,必须使用 swoole_process::wait(); 全部的子进程才能收到主进程的信息?否则就只能和第一个进程通讯? pleas help me !!
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2016-11-18
自己顶上去
赞
0
回复
2016-12-24
c
c'est la vie
已知BUG导致,子进程结束会销毁消息队列,请使用最新的1.7.19-rc2版本。
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。