在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
echo '开始时间:'.date('H:i:s',time()); //进程数 $work_number=6; // $worker=[]; //模拟地址 $curl=[ 'https://blog.csdn.net/feiwutudou', 'https://wiki.swoole.com/wiki/page/215.html', 'http://fanyi.baidu.com/?aldtype=16047#en/zh/manager', 'http://wanguo.net/Salecar/index.html', 'http://o.ngking.com/themes/mskin/login/login.jsp', 'https://blog.csdn.net/marksinoberg/article/details/77816991' ]; //单线程模式 // foreach ($curl as $v) { // echo curldeta($v); // } //创建进程 for ($i=0; $i < $work_number; $i++) { //创建多线程 $pro=new swoole_process(function(swoole_process $work) use($i,$curl){ //获取html文件 $content=curldeta($curl[$i]); //写入管道 $work->write($content.PHP_EOL); },true); $pro_id=$pro->start(); $worker[$pro_id]=$pro; } //读取管道内容 foreach ($worker as $v) { echo $v->read().PHP_EOL; } //模拟爬虫 function curldeta($curl_arr) { //file_get_contents echo $curl_arr.PHP_EOL; file_get_contents($curl_arr); } //进程回收 swoole_process::wait(); echo '结束时间:'.date('H:i:s',time()); /** * 创建多进程 */ $worker_num = 6; // 默认进程数 $workers = []; // 进程保存 $redirect_stdout = false; // 重定向输出 ; 这个参数用途等会我们看效果 for($i = 0; $i < $worker_num; $i++){ $process = new \swoole_process('callback_function', $redirect_stdout); // 启用消息队列 int $msgkey = 0, int $mode = 2 $process->useQueue(0, 2); $pid = $process->start(); // 管道写入内容 $process->write('index:'.$i); $process->push('进程的消息队列内容'); // 将每一个进程的句柄存起来 $workers[$pid] = $process; } /** * 子进程回调 * @param swoole_process $worker [description] * @return [type] [description] */ function callback_function(\swoole_process $worker) { $recv = $worker->pop(); echo "子输出主内容: {$recv}".PHP_EOL; //get guandao content $recv = $worker->read(); $result = doTask(); echo PHP_EOL.$result.'==='.$worker->pid.'==='.$recv; $worker->exit(0); } /** * 监控/回收子进程 */ while(1){ $ret = \swoole_process::wait(); if ($ret){// $ret 是个数组 code是进程退出状态码, $pid = $ret['pid']; echo PHP_EOL."Worker Exit, PID=" . $pid . PHP_EOL; }else{ break; } } /** * doTask * @return [type] [description] */ function doTask() { sleep(2); return true; }
|
2022-08-16
2022-11-06
2022-08-18
2022-08-15
2022-08-18
请发表评论