开发者

How do I set time limit for loop?

开发者 https://www.devze.com 2023-02-03 19:56 出处:网络
I do have a loop, say: $lines = file(\'file.txt\'); foreach ($lines as $line) { // some function } Some times this take more time to complete one loop if data is not available. So how do I set

I do have a loop, say:

$lines = file('file.txt');    

foreach ($lines as $line) { 

  // some function

}

Some times this take more time to complete one loop if data is not available. So how do I set a time limit to each loop so if any info isn't available it will move to nex开发者_JAVA百科t?


You should not solve this with a time limit at all! Using a time limit can create all kinds of problems in the long run. I'd go as far as to say "time limits are a code smell".

Instead check first, if data is available, if not, don't loop.


How you go about it depends on what's inside the loop.

If its a nested loop and the long duration arises due to the number of iterations on the inner loop, then you can detect this in your code e.g.:

foreach ($lines as $line) { 
    $iters=strtok($line, ',');
    $start_iter=time();
    for ($x=0; $x<$iters; $x++) {
       ... do something here
       if (time()-$start_iter>600) {
          print "abandoning $line after $x iterations\n";
          break;
       }
    }
}

OTOH, if the function is essentially atomic, e.g.

foreach ($lines as $line) {
       mysql_query($line);
}

Then control will never return to your code until the operation completes. However that does not mean that its impossible to interrupt the processing and continue to the next line - there are 2 ways to do this:

1) use pcntl_alarm to trigger a signal handler - note that of itself this would not solve the problem in the case above - since on completion of the signal handler, the function called would resume - it might be possible in some case to force premature termination of the lopped function.

2) run the inner part of the loop in a seperate process and kill the process if it overruns.

Have a look at the pcntl functions for more details - note these only work in Linux/Unix/POSIX environments.


Store the current time with time() before the loop and compare it in the beginning of the loop to the current time and if a set time has passed, break.

As tharkun said, though, you should first check if data is available. filemtime() comes to mind if the file is changed externally.


You should test to see if the data is available instead of creating a timer.

$lines = file('file.txt');

if (!empty($lines)) {
  foreach ($lines as $line) {
    // some function
  }
}


You simply need to check if there's any data in the $lines array before you try to iterate over it using the foreach. For example:

<?php
    $lines = file('file.txt');
    if (!empty($lines)) {
      foreach ($lines as $currentLine) {
          ...
      }
    }
?>

That said, for large files (where the array generated by using file might not fit in memory), you should use a while loop to read a line at a time until the end of file (EOF) marker is found.

For example:

<?php
    $fileHandle = fopen("test.txt");
    if(is_resource($fileHandle)) {
        while (!feof($fileHandle )) {
            $currentLine = fread($fileHandle);
        }
    fclose($handle);
    }
?>


According to this code, if data is not available, loop did not starts at all! So, question need to be more specific.

0

精彩评论

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

关注公众号