php mysql 线程安全_PHP 实现多线程方式? 比如pthreads ?

这篇博客通过实例展示了PHP中使用cURL进行多线程并发请求与串行请求的区别。在示例中,两个接口串行访问需要2秒,而并行访问只需约2秒。文章详细分析了请求过程中的时间消耗,并提供了请求的响应信息和执行时间,揭示了多线程请求在效率上的优势。
摘要由CSDN通过智能技术生成

接口1: php -S 127.0.0.1:8080 -t /home/eechen/www

接口2: php -S 127.0.0.2:8080 -t /home/eechen/www

/home/eechen/www/index.php:

header('Content-Type: application/json; charset=utf-8');

echo json_encode(array('SERVER_NAME' => $_SERVER['SERVER_NAME']));

//串行访问需要sum(2,1)秒,并行访问需要max(2,1)秒.($_SERVER['SERVER_NAME'] == '127.0.0.1') ? sleep(2) : sleep(1);

?>/home/eechen/www/curl.php:<?php $url[] = 'http://127.0.0.1:8080';$url[] = 'http://127.0.0.2:8080';$mh = curl_multi_init();foreach($url as $k => $v) {$ch[$k] = curl_init($v);curl_setopt($ch[$k], CURLOPT_HEADER, 0); //不输出头curl_setopt($ch[$k], CURLOPT_RETURNTRANSFER, 1); //exec返回结果而不是输出,用于赋值curl_multi_add_handle($mh, $ch[$k]); //决定exec输出顺序}$running = null;$starttime = microtime(true);//执行批处理句柄(类似pthreads多线程里的start开始和join同步)do {//CURLOPT_RETURNTRANSFER如果为0,这里会直接输出获取到的内容.如果为1,后面可以用curl_multi_getcontent获取内容.curl_multi_exec($mh, $running);//阻塞直到cURL批处理连接中有活动连接,不加这个会导致CPU负载超过90%.curl_multi_select($mh);} while ($running > 0);echo microtime(true) - $starttime."\n"; //耗时约2秒foreach($ch as $v) {$info[] = curl_getinfo($v);$json[] = curl_multi_getcontent($v);curl_multi_remove_handle($mh, $v);}curl_multi_close($mh);var_export($json);var_export($info);?>//输出:2.0015449523926array (0 => '{"SERVER_NAME":"127.0.0.1"}',1 => '{"SERVER_NAME":"127.0.0.2"}',)array (0 =>array ('url' => 'http://127.0.0.1:8080/','content_type' => 'application/json; charset=utf-8','http_code' => 200,'header_size' => 107,'request_size' => 53,'filetime' => -1,'ssl_verify_result' => 0,'redirect_count' => 0,'total_time' => 2.0013990000000002,'namelookup_time' => 5.3999999999999998E-5,'connect_time' => 0.00015799999999999999,'pretransfer_time' => 0.000194,'size_upload' => 0,'size_download' => 27,'speed_download' => 13,'speed_upload' => 0,'download_content_length' => -1,'upload_content_length' => 0,'starttransfer_time' => 0.00079699999999999997,'redirect_time' => 0,'certinfo' =>array (),'primary_ip' => '127.0.0.1','primary_port' => 8080,'local_ip' => '127.0.0.1','local_port' => 57653,'redirect_url' => '',),1 =>array ('url' => 'http://127.0.0.2:8080/','content_type' => 'application/json; charset=utf-8','http_code' => 200,'header_size' => 107,'request_size' => 53,'filetime' => -1,'ssl_verify_result' => 0,'redirect_count' => 0,'total_time' => 1.0012369999999999,'namelookup_time' => 1.1E-5,'connect_time' => 4.6999999999999997E-5,'pretransfer_time' => 6.3E-5,'size_upload' => 0,'size_download' => 27,'speed_download' => 26,'speed_upload' => 0,'download_content_length' => -1,'upload_content_length' => 0,'starttransfer_time' => 0.00063699999999999998,'redirect_time' => 0,'certinfo' =>array (),'primary_ip' => '127.0.0.2','primary_port' => 8080,'local_ip' => '127.0.0.1','local_port' => 43645,'redirect_url' => '',),)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值