linux 下 php set_time_limit 不起作用,PHP为系统调用的脚本设置超时,set_time_limit不起作用...

小编典典

您可以结合使用“ –timeout”和time()。首先确定总时间,然后在脚本运行时降低–timeout。

例如:

$endtime = time()+15;

foreach( $url as $key => $value){

$timeleft = $endtime - time();

if($timeleft > 0) {

$wget = "wget -t 1 --timeout $timeleft $otherwgetflags $value";

print "running $wget
";

system($wget);

} else {

print("timed out!");

exit(0);

}

}

注意:如果不使用-t,wget将尝试20次,每次等待–timeout秒。

这是使用proc_open / proc_terminate(@Josh的建议)的一些示例代码:

$descriptorspec = array(

0 => array("pipe", "r"),

1 => array("pipe", "w"),

2 => array("pipe", "w")

);

$pipes = array();

$endtime = time()+15;

foreach( $url as $key => $value){

$wget = "wget $otherwgetflags $value";

print "running $wget\n";

$process = proc_open($wget, $descriptorspec, $pipes);

if (is_resource($process)) {

do {

$timeleft = $endtime - time();

$read = array($pipes[1]);

stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);

if(!empty($read)) {

$stdout = fread($pipes[1], 8192);

print("wget said--$stdout--\n");

}

} while(!feof($pipes[1]) && $timeleft > 0);

if($timeleft <= 0) {

print("timed out\n");

proc_terminate($process);

exit(0);

}

} else {

print("proc_open failed\n");

}

}

2020-06-03

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值