php swoole 使用多进程爬取网址示例。
<?php $s_time = time(); echo '开始时间:' . date('H:i:s', $s_time) . PHP_EOL; //进程数组 $worker = []; //模拟地址 $curl = [ 'https://www.baidu.com/', 'https://www.phpernote.com/', 'https://www.taobao.com/', 'https://www.jd.com/', 'https://www.163.com' ]; //模拟爬虫 function curldeta($curl_arr) { return file_get_contents($curl_arr); } //传统读取模式 /*for ($i = 0; $i < count($curl); $i++) { echo curldeta($curl[$i]); }*/ //创建进程 for ($i = 0; $i < count($curl); $i++) { //创建多线程 $pro = new swoole_process(function (swoole_process $work) use ($i, $curl) { //获取网页内容 $content = curldeta($curl[$i]); //写入管道 $work->write($content . PHP_EOL); }, true); $pro_id = $pro->start(); $worker[$pro_id] = $pro; } //读取管道内容 foreach ($worker as $v) { echo date('Y-m-d H:i:s') . $v->read() . PHP_EOL; } //进程回收 swoole_process::wait(); $e_time = time(); echo '结束时间:' . date('H:i:s', $e_time) . PHP_EOL; echo '所用时间:' . ($e_time - $s_time) . '秒' . PHP_EOL;