1

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?

2
  • You need to show more of your code, as your question is impossible to answer at the moment. "some times this take more time to complete one loop if data is not available" does not make sense with regard to your current posted code. Commented Jan 13, 2011 at 10:09
  • i'm sorry, but i don't understand the question. could you please elaborate? Commented Jan 13, 2011 at 10:10

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I talking about function inside the loop...actually the function inside the loop taking too much time.
In that case you should show us the function inside the loop. BUT anyways, using time limits in code is most often a code smell = bad.
2

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.

Comments

1

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.

Comments

1

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

1 Comment

oops I forgot to tell you....the function I call is taking much time !!! there is no empty lines in file....so I need to restrict function in loop for particular time frame...
1

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

3 Comments

oops I forgot to tell you....the function I call is taking much time !!! there is no empty lines in file....so I need to restrict function in loop for particular time frame...
@matthew - In that case the second approach is ideal - you can simply read a lines at a time until a certain time limit has expired and then use fseek to jump back to the previous position (you'll need to store the bytes read as you go in a DB or file, etc.) the next time the script runs.
but how do I implement with this code?? I dont have worries with file memory problem coz I set memory in the beginning of loop.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.