PHP使用swoole实现多线程爬虫
php  /  管理员 发布于 8年前   335
在swoole中,php可以借助其启动子进程的方式,实现php的多进程:
<?php$s_time = time();echo '开始时间:'.date('H:i:s',$s_time).PHP_EOL;//进程数$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(); $e_time = time();echo '结束时间:'.date('H:i:s',$e_time).PHP_EOL; echo '所用时间:'.($e_time-$s_time).'秒'.PHP_EOL;?>多线程执行结果:

作为对比,单线程结果:

提升十分明显!
更多PHP相关知识,请访问PHP教程!
以上就是PHP使用swoole实现多线程爬虫的详细内容,更多请关注其它相关文章!
test1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
