当前位置:首页 > PHP > 正文内容

PHP模拟并发请求

高老师9年前 (2017-07-30)PHP3645

原理:使用curl_init()创建多个请求实例,再使用curl_multi_init()批量执行创建的多个请求实例。

文件1:curl.php

<?php 

$threads=500;//并发请求次数
$url='http://blog.cn/index.php?';//请求的url

//创建一个未定义的curl句柄数组
$ch=array();

//创建批处理cURL的句柄
$mh = curl_multi_init();


//创建并发请求次数个url用于后面给curl分配
for ($i=0; $i <$threads ; $i++) {
		//有多少请求,创建多少curl会话
		$ch[$i]=curl_init();
		curl_setopt($ch[$i], CURLOPT_URL, $url.rand(1,1000));//随机参数,避免缓存
		curl_setopt($ch[$i], CURLOPT_HEADER, 0);		
		//创建的会话分配给curl批处理句柄
		curl_multi_add_handle($mh,$ch[$i]);
}


$running=null;
//所有的curl会话分配给$mh这个curl批量处理句柄来执行
do {
    usleep(10000);
    curl_multi_exec($mh,$running);
} while ($running > 0);


//关闭已经创建的会话句柄
for ($i=0; $i <$threads ; $i++) { 
	curl_multi_remove_handle($mh, $ch[$i]);
}

//关闭批处理句柄
curl_multi_close($mh);

?>

文件2:index.php

<?php 
  file_put_contents('1.txt',date('Y-m-d H:i:s',time())."\r\n",FILE_APPEND );
?>

文件3:1.txt

2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:06
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07
2017-07-30 22:30:07

基本同时请求的还是比较多的,受带宽和CPU影响,多线程的访问,并不是一定会同时,线程是否立即执行决定权是CPU

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/37.html

分享给朋友:

“PHP模拟并发请求” 的相关文章

php将html转为pdf,php将html页面导出pdf

php将html转为pdf,php将html页面导出pdf

首先下载wkhtmltox-0.12.4_linux-generic-amd64.tar.xz   (不要下载RPM包,依赖太多,需要x-server支持),并解压,执行测试运行正常tar wkhtmltox-0.12.4_linux-generic-amd64.tar.xzcd...

php无限查询下级,php递归统计下级总数,php 获取无限子级

php无限查询下级,php递归统计下级总数,php 获取无限子级

因项目需要需要统计用户无限下级计算分销佣金,计算每月分红,计算无限下级团队的业绩,需要先获得某个会员的无限下级成员。先看看数据库中的member表字段id            username       &...

php 开启错误提示,php 关闭错误提示

php 开启错误提示,php 关闭错误提示

开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...

php数组合并 array_merge和+号的区别

php数组合并 array_merge和+号的区别

array_merge是最常用的数组合并方法,+号同样也可以,但是却有很大不同。array_merge遇到相同字符串key,后面数组的key会覆盖前面数组的key,+号正好相反。$a = [ 'one' => 'A on...

cookie跨域,cookie p3p跨域

cookie跨域,cookie p3p跨域

最近在公司开发一个新的项目假设项目域名是a.com,需要接入b.com的单点登陆系统。(1).首先我们会在a.com的登陆页面用iframe引入b.com来显示登陆界面,实际上登陆验证操作都是在b.com上面(2).当b.com验证通过,会在前端ajax请求a.com的回调地址,这个回调地址目的就是...

thinkphp3定时任务,tp定时任务,thinkphp定时任务

thinkphp3定时任务,tp定时任务,thinkphp定时任务

本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask由于tp3.2.x官方开发未考虑命令行支持和绝对路径开发的标准,因此我编写了一个支持的类来运行。1.在tp3.2.3根目录下安装easytaskcomposer&...