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

时间:2011-09-21 15:48:57

标签: php

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

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

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

2 个答案:

答案 0 :(得分:3)

最简单的方法是在脚本开头设置一个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;
    }
 }

答案 1 :(得分:1)

$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 ;
  } 
相关问题