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

PHP模拟并发请求

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

原理:使用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 mysql 行锁,php mysql 行级锁,php mysql 行锁定

php mysql 行锁,php mysql 行级锁,php mysql 行锁定

应用场景:PHP模拟购买,商品数量大于0才能购买常见代码:<?php //连接数据库 $con=mysqli_connect("localhost","ihuohuo","927464cy","ihuohuo");...

cookie跨域,cookie p3p跨域

cookie跨域,cookie p3p跨域

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

php jsonp转json,php jsonp转数组,php jsonp转对象

php jsonp转json,php jsonp转数组,php jsonp转对象

将jsonp转为PHP数组和对象。/**  * jsonp转数组|Jsonp转json  * @param string $jsonp jsonp字符串  * @param bool $as...

在MyISAM引擎中使用事务会怎样

在MyISAM引擎中使用事务会怎样

众所周知MyISAM引擎不支持事务,但是我只是知道不支持事务,并未测试具体的表现是什么,测试代码如下:try {     //开启事务     Db::startTrans();    &...

tp orm事务提交未执行的教训和总结

tp orm事务提交未执行的教训和总结

最近在项目中处理一个关于商品数据重复需要删除多余的商品记录,但是删除一条商品必然要把关联的其他表商品的id和其他商品信息更换为正确的,删除一个商品记录,同时要去修改100多张表的关联商品数据,在项目中引用了tp orm 1.2版本,由于项目是php5.6版本,没法使用最新orm,在代码中每处理1个商...

php生成器yield from详解

php生成器yield from详解

PHP7中,通过生成器委托(yield from),可以将其他生成器、可迭代的对象、数组委托给外层生成器。外层的生成器会先顺序 yield 委托出来的值,然后继续 yield 本身中定义的值。同时yield from也能获取到生成器的返回值...