这是一个创建多进程来协同并发同时抓取数据汇总的示例代码:
<?php
$url_arr = array();
for ($i=0;$i<10;$i++){
$url_arr[] = "www.baidu.com?wd=".$i;
}
echo "开始执行:".date("Y-m-d H:i:s").PHP_EOL;
$_begintime=time();
$workers = array();
for ($i=0;$i<5;$i++){
$process = new swoole_process('getContents',true);
$process->start();
$process->write($i);
$workers[] = $process;
}
//主进程数据结果
foreach ($workers as $process){
echo $process->read();
echo PHP_EOL;
}
$_finishtime=time();
echo "任务全部完成:".date("Y-m-d H:i:s").",总耗时".($_finishtime-$_begintime).PHP_EOL;
function getContents(swoole_process $worker){
$_begintime=time();
$i = $worker->read();
global $url_arr;
// echo "1";
$res1 = execCurl($url_arr[($i*2)]);
$res2 = execCurl($url_arr[($i*2+1)]);
$_finishtime=time();
// $worker->write('Hello');
echo $res1.PHP_EOL.$res2."耗时:".($_finishtime-$_begintime);
}
function execCurl($url){
sleep(rand(1,10));
return "抓取网页 ".$url." 完成,";
}
网友回复