您可以结合使用“ –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"); }}


