开发者

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

I have a script that pulls from an external database and stores the data in a local database. I'm setup on a shared-server environment so I'm not able to let the script run for longer than two minutes, yet it would take开发者_JAVA百科 around 5-10 minutes to run to completion.

Can I have the script stop its foreach loop after one and a half minutes, so I can redirect to the same script but with a different database offset to pick up where it left?

I can do the last part of that using a GET query string, but I'm unsure how to time it.


The easiest way would be to set a time() at the start of your script and check the difference in your foreach loop.

 $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 ;
  } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜