mathew
mathew

Reputation: 65

How do I set time limit for loop?

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 next?

Upvotes: 1

Views: 3530

Answers (6)

symcbean
symcbean

Reputation: 48357

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.

Upvotes: 2

markus
markus

Reputation: 40675

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.

Upvotes: 5

MDI
MDI

Reputation: 177

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

Upvotes: 1

John Parker
John Parker

Reputation: 54445

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);
    }
?>

Upvotes: 1

Ted
Ted

Reputation: 2239

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
  }
}

Upvotes: 1

Shadikka
Shadikka

Reputation: 4276

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.

Upvotes: 1

Related Questions