PHP timeout情况_PHP: set_time_limit - Manual

Both set_time_limit(...) and  ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:

my_exec.php:

{$map=array('"'=>'\"','$'=>'\$','`'=>'\`','\\'=>'\\\\','!'=>'\!');$str_requires=strtr($str_requires,$map);$path_run=dirname($_SERVER['SCRIPT_FILENAME']);$my_target_exec="/usr/bin/php -r \"chdir('{$path_run}');{$str_requires}\\\$params=json_decode(file_get_contents('php://stdin'),true);call_user_func_array('{$function_name}', \\\$params);\"";$my_target_exec=strtr(strtr($my_target_exec,$map),$map);$my_background_exec="(/usr/bin/php -r \"chdir('{$path_run}');{$str_requires}my_timeout_exec(\\\"{$my_target_exec}\\\", file_get_contents('php://stdin'),{$timeout});\"

}

functionmy_timeout_exec($cmd,$stdin='',$timeout)

{$start=time();$stdout='';$stderr='';//file_put_contents('debug.txt', time().':cmd:'.$cmd."\n", FILE_APPEND);

//file_put_contents('debug.txt', time().':stdin:'.$stdin."\n", FILE_APPEND);$process=proc_open($cmd, [['pipe','r'], ['pipe','w'], ['pipe','w']],$pipes);

if (!is_resource($process))

{return array('return'=>'1','stdout'=>$stdout,'stderr'=>$stderr);

}$status=proc_get_status($process);posix_setpgid($status['pid'],$status['pid']);//seperate pgid(process group id) from parent's pgidstream_set_blocking($pipes[0],0);stream_set_blocking($pipes[1],0);stream_set_blocking($pipes[2],0);fwrite($pipes[0],$stdin);fclose($pipes[0]);

while (1)

{$stdout.=stream_get_contents($pipes[1]);$stderr.=stream_get_contents($pipes[2]);

if (time()-$start>$timeout)

{//proc_terminate($process, 9);    //only terminate subprocess, won't terminate sub-subprocessposix_kill(-$status['pid'],9);//sends SIGKILL to all processes inside group(negative means GPID, all subprocesses share the top process group, except nested my_timeout_exec)

//file_put_contents('debug.txt', time().":kill group {$status['pid']}\n", FILE_APPEND);return array('return'=>'1','stdout'=>$stdout,'stderr'=>$stderr);

}$status=proc_get_status($process);//file_put_contents('debug.txt', time().':status:'.var_export($status, true)."\n";if (!$status['running'])

{fclose($pipes[1]);fclose($pipes[2]);proc_close($process);

return$status['exitcode'];

}usleep(100000);

}

}?>

a_class.php:

static functionjack($a,$b)

{sleep(4);file_put_contents('debug.txt',time().":A::jack:".$a.' '.$b."\n",FILE_APPEND);sleep(15);

}

}?>

test.php:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 中的字典和列表来实现这个功能。具体的做法如下: 1. 遍历列表中的每个字典,将time_tag作为键,update_time作为值,存储到一个新的字典中。 2. 对新字典中的每个键进行去重,只保留一个,这里可以使用 set() 函数。 3. 遍历去重后的键,对于每个键,在原来的列表中找到最大的update_time对应的字典,将其存储到一个新的列表中。 示例代码如下: ```python data = [{'time_tag': '2023051706', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051706', 'kp_3h': '0.000', 'ap_3h': '0', 'update_time': '20230517064024'}, {'time_tag': '2023051706', 'kp_3h': '0.333', 'ap_3h': '2', 'update_time': '20230517065422'}, {'time_tag': '2023051709', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051712', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051715', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051718', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051721', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}] # 存储每个time_tag对应的最大update_time的字典 max_dict = {} for d in data: time_tag = d['time_tag'] update_time = int(d['update_time']) if time_tag not in max_dict or max_dict[time_tag] < update_time: max_dict[time_tag] = update_time # 对time_tag去重 unique_time_tag = set(max_dict.keys()) # 存储最终结果 result = [] for time_tag in unique_time_tag: max_update_time = max_dict[time_tag] for d in data: if d['time_tag'] == time_tag and int(d['update_time']) == max_update_time: result.append(d) break # 输出最终结果 print(result) ``` 运行结果为: ``` [{'time_tag': '2023051706', 'kp_3h': '0.333', 'ap_3h': '2', 'update_time': '20230517065422'}, {'time_tag': '2023051709', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051712', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051715', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051718', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}, {'time_tag': '2023051721', 'kp_3h': '-1.000', 'ap_3h': '-1', 'update_time': '20230517000918'}] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值