栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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<br>";    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 $wgetn";  $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 outn");      proc_terminate($process);      exit(0);    }  } else {    print("proc_open failedn");  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405744.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号