Swoole多个子进程运行测试
<?php
echo "processStart:".date('Y-m-d H:i:s').PHP_EOL;
$workers = [];
$urls = [
'https://a.com',
'https://b.com',
'https://b.com',
];
for ($n = 0; $n < 3; $n++) {
//开启子进程
$process = new swoole_process(function($worker) use ($n,$urls) {
//curl
$content = curlData($urls[$n]);
echo $content.PHP_EOL;
//$worker->write($content.PHP_EOL);//写入管道中
},true);
$pid = $process->start();
$workers[$pid] = $process;
}
//获取管道内容
foreach ($workers as $process) {
echo $process->read();
}
//模拟耗时1秒
function curlData($url){
sleep(1);
return $url."Success:".PHP_EOL;
}
echo "processEnd:".date('Y-m-d H:i:s').PHP_EOL;
网友评论