<?php //进程仓库 $workers=[]; //最大进程数 $worker_num=2; //批量创建进程 for($i=0;$i<$worker_num;$i++){ //创建子进程 $process=new swoole_process("doProcess",false,false); //开启队列--类似于全局函数 $process->useQueue(); //启动进程并获取进程id $pid=$process->start(); //存入进程数组 $workers[$pid]=$process; } //创建进程对应的执行函数 function doProcess(swoole_process $process){ $recv=$process->pop();//8192 echo "从主进程获取到数据:$recv \n"; sleep(5); $process->exit(0); } //主进程向子进程添加数据 foreach($workers as $pid=>$process){ $process->push("Hell 子进程 $pid \n"); } //等待子进程结束回收资源 for($i=0;$i<$worker_num;$i++){ $ret=swoole_process::wait();//等待执行完成 $pid=$ret[‘pid‘]; unset($workers[$pid]); echo "子进程$pid\n"; } ?>