<?php
echo posix_getpid().PHP_EOL;
$info = array(
"sendmail"=>1,
"mailto"=>"12345@qq.com",
"sendsms"=>1,
"smsto"=>"123456"
);
echo "start:".date("Y-m-d H:i:s").PHP_EOL;
$mail_process = new swoole_process('sendMail',true);
$mail_process->name('mail pro');
$mail_process->start();
$sms_process = new swoole_process('sendSMS',true);
//设置进程名(mac不支持)
$mail_process->name('sms pro');
$sms_process->start();
//主进程输出子进程范围内容
echo $mail_process->read();
echo PHP_EOL;
echo $sms_process->read();
echo PHP_EOL;
echo "end:".date("Y-m-d H:i:s").PHP_EOL;
//并行函数
function sendMail(swoole_process $worker){
global $info;
if($info['sendmail']==1){
$worker->write("{$worker->pid}send mail to ".$info['mailto']);//管道通信
}
}
function sendSMS(swoole_process $worker){
global $info;
if($info['sendmail']==1){
sleep(2);
$worker->write("{$worker->pid}send sms to ".$info['smsto']);
}
}
//添加这个作为监护进程.这样子进程执行完了父进程不会退出
swoole_process::signal(SIGCHLD, function($sig) {
echo posix_getpid().PHP_EOL;
//必须为false,非阻塞模式
while($ret = swoole_process::wait(false)) {
print_r($ret);
echo "PID={$ret['pid']}".PHP_EOL;
}
});
swoole多进程
最新推荐文章于 2024-03-21 22:38:51 发布