创建 多进程
<?php
echo PHP_EOL . time() ;
$worker_num =3;//创建的进程数
for($i=0;$i<$worker_num ; $i++){
$process = new swoole_process('callback_function_we_write');
$pid = $process->start();
}
function callback_function_we_write(swoole_process $worker){
for($i=0;$i<100000000;$i++){}
echo PHP_EOL . time() ;
}
管道通讯
<?php
$redirect_stdout = true;// 重定向输出 注意 这次我改成 true 了,其他没变
$worker_num = 2;//进程数量
$workers = [];//存放进程用的
for($i = 0; $i < $worker_num; $i++){
$process = new swoole_process('workerFunc',$redirect_stdout );
$pid = $process->start();
$workers[$pid] = $process;//将每一个进程的句柄存起来
}
// 这里是主进程哦。
foreach($workers as $pid => $process){// $process 是子进程的句柄
$process->write("hello worker[$pid]\n");//子进程句柄向自己管道里写内容 $process->write($data);
echo "From Worker: ".$process->read();//子进程句柄从自己的管道里面读取信息 $process->read();
echo PHP_EOL.PHP_EOL;
}
function workerFunc(swoole_process $worker){//这里是子进程哦
$recv = $worker->read();
echo PHP_EOL. "From Master: $recv\n";
//send data to master
$worker->write("hello master , this pipe is ". $worker->pipe ."; this pid is ".$worker->pid."\n");
sleep(2);
$worker->exit(0);
}
转载请注明:(●--●) Hello.My Weicot » Swoole 多进程 以及进程通信示例