标签:com 例子 成功 out cti roc 控制台 lse 图片
<?php /** 这是一个swoole父进程和子进程之间通信的例子 */ //进程创建成功后回调处理 function handle(swoole_process $worker){ //从进程管道中读取数据 $data = $worker->read(); echo PHP_EOL."from master: {$data}"; //向进程管道中写入数据 $pipe = $worker->pipe;//子进程的管道编号 $pid = $worker->pid;//子进程的PID $worker->write("hello master, this pipe is {$pipe}, pid is {$pid}"); sleep(2); $worker->exit(0); } //进程数量 $worker_num = 2; //重定向输入输出 $redirect_stdin_stdout = false; //存放进程的数组 $workers = []; //循环创建多进程 for($i=0; $i<$worker_num; $i++){ //创建进程 $process = new swoole_process("handle", $redirect_stdin_stdout); //启动进程 $pid = $process->start(); //保存进程句柄 $workers[$pid] = $process; } //主进程 foreach($workers as $pid=>$process){ //子进程句柄向自己的管道中写入数据 $process->write("hello worker, this pid is {$pid}"); //子进程句柄从自己的管道中读取数据 $data = $process->read(); echo PHP_EOL."from worker: {$data}".PHP_EOL; }
from master: hello worker, this pid is 59673
from worker: hello master, this pipe is 3, pid is 59673
from master: hello worker, this pid is 59675
from worker: hello master, this pipe is 7, pid is 59675
参考https://www.jianshu.com/p/f89d5ca7b29d
子进程的编号pid,可以通过ssh控制台的top命令直接可以看到。
标签:com 例子 成功 out cti roc 控制台 lse 图片
原文地址:https://www.cnblogs.com/tochw/p/12977677.html