-
全部PHP代码:
/*
* 利用exec实现非阻塞请求,提高请求20%的容量
* 1. php.ini需要去除disable_functions=exec来开启可使用exec函数
* 2. 利用了“命令行+api+参数”的请求过程,最终返回api的结果
* 3. 注意大多数命令行win与linux的不同,混用可能会报错
*
* exec_non_blocking($api, 参数键值对数组, 标记运行了什么函数)
* */
function exec_non_blocking($api, $data_array, $sign){
$sign = $sign?$sign:get_millisecond();
$data = '';
foreach ($data_array as $key=>$value){
$data = $data.$key.'='.$value.'&';
}
if (function_exists('exec')){
try{
exec("curl -d '$data' '$api'", $_out); // 参数、api都应该加引号
$out = $_out[0];
if (is_json($out)){
$back = json_to_array($out);
}else{
$back = $out;
}
}catch (Exception $error){
$back = $error;
}
}else{
$back = 'php中的exec()函数未开启,请在php.ini需要去除disable_functions=exec来开启可使用exec()函数。';
}
return ['exec_data'=>$back, 'sign'=>$sign, 'test_data'=>[$api, $data_array], 'curl_way'=>'post']; // 统一返回json或string
}
// 测试
$data_array = [
'debug_key'=>2
];
$sign = 0;
$back = exec_non_blocking('http://192.168.131.133/laravel60/public/index.php/admin/login_check', $data_array, $sign);
print_r($back);
/*开始-依赖函数*/
function get_millisecond() {
list($microsecond , $time) = explode(' ', microtime()); //' '中间是一个空格
return (float)sprintf('%.0f',(floatval($microsecond)+floatval($time))*1000);
}
function is_json($data = '', $assoc = false) {
$data = json_decode($data, $assoc);
if (($data && is_object($data)) || (is_array($data) && !empty($data))) {
return true;
}
return false;
}
function json_to_array($object_data){
return json_decode(json_encode($object_data),true);
}
/*结束-依赖函数*/
-
请求的结果:
1)命令行查看:
[root@localhost test]# php test8.php
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 150 0 138 100 12 792 68 --:--:-- --:--:-- --:--:-- 797
Array
(
[exec_data] => {"state":0,"msg":"请先登录..","login_href":"login.php?login=must","content":{"0":null,"1":null,"input":{"debug_key":"2"},"post":true}}
[sign] => 1570673973608
[test_data] => Array
(
[0] => http://192.168.131.133/laravel60/public/index.php/admin/login_check
[1] => Array
(
[debug_key] => 2
)
)
[curl_way] => post
)
[root@localhost test]#
2)网页查看:
-
注意点:
1)disable_functions=exec 开启后要重启php服务才能奏效;
2)exec是将同步分布到命令行,分布到命令行后原请求不需要等待命令行的结束而结束,命令行会继续运行直到exec里的api运行完毕,从而实现非阻塞;
3)api请求方式为post。
-