在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+MYSQL架设,程序商业模式为目前最为火爆的包月订制包年服务模式,这种包年订购在国外网站已经热火很多年了,并且已经发展到一定规模,像英国的男士用品网站BlackSocks,一年的袜子购买量更是达到了1000万双。功能:1、实现多产品上线,2、不用注册也可以直接下单购买,3、集成目前主流支付接口,4、下单发货均有邮件提醒。
0
立即学习“PHP免费学习笔记(深入)”;
提升十分明显!
更多PHP相关知识,请访问PHP教程!
以上就是PHP使用swoole实现多线程爬虫的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号