在一定的查询/解析时间后停止脚本(没有致命错误)


Stop a script (without fatal error) after a certain amount of query/parse time

我有一个脚本,从外部数据库中提取数据并将数据存储在本地数据库中。我是在共享服务器环境中设置的,所以我不能让脚本运行超过两分钟,但它需要大约5-10分钟才能运行完成。

我可以让脚本在一分半钟后停止它的foreach循环,这样我就可以重定向到相同的脚本,但使用不同的数据库偏移量来拾取它离开的地方?

我可以使用GET查询字符串做最后一部分,但我不确定如何计时。

最简单的方法是在脚本开始时设置time(),并在foreach循环中检查差异。

 $start = time(); // Returns time in seconds
 foreach($bigdata as $row) {
    if(time()-$start > 100) {  // Stop 20 seconds before 120 sec limit
       // Some code for exiting the loop...
       break;
    }
 }
$total_time = 0;
$start_time = microtime(true);
while($total_time < 60)//run while less than a minute
  {
    //DoSomething;
    echo $total_time."'n";
    sleep(5);//wait amount in seconds
  $total_time =  microtime(true) - $start_time ;
  } 
相关文章: