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

PHP模拟并发请求

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

原理:使用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 header属性,php header 详解,php header的作用

php header属性,php header 详解,php header的作用

header() 函数向客户端发送原始的 HTTP 报头。(官方解释)通俗的讲header函数将参数中的字符串作为服务端的响应头来返回给客户端。什么是服务端的响应头?打开谷歌浏览器看看network中的请求response header信息即可。更多的参数百度response header即可浏览器...

 php xml字符串转数组,phpxml转数组,php 将xml转换成数组

php xml字符串转数组,phpxml转数组,php 将xml转换成数组

001源码:/*  * $xml_str是xml字符串  */ function  xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...

php上传大文件,php大文件上传

php上传大文件,php大文件上传

(1).前端文件:<form action="upload.php" method="post" enctype="multipart/form-data">    &...

php mcrypt扩展被废弃的解决方案

php mcrypt扩展被废弃的解决方案

使用openssl扩展对应替换mcrypt的函数,(比较麻烦,但是openssl是未来趋势)在新版php中编译mcrypt扩展使用一个纯php代码实现的mcrypt扩展库,git地址为https://github.com/phpseclib/mcrypt_compat,每个mcrypt的方法都已经实...

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

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

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

windows安装php event扩展问题

windows安装php event扩展问题

php event扩展在windows中依赖于php_sockets扩展,因此在php.ini中必须先加载php sockets扩展,如下。extension=sockets extension=event...