Continue while loop after a certain time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Continue while loop after a certain time

Post by Grim... »

I've got a while loop that performs a lot of calculations.
If the calculations take more than (say) 2 seconds, I want the loop to give up and move on to the next item (ie. continue).
Is this possible?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hmm... sleep() won't work.

Maybe set_time_limit() and register_shutdown_function() could be of help? Not sure on specifics off the top of my head.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Continue while loop after a certain time

Post by aerodromoi »

Grim... wrote:I've got a while loop that performs a lot of calculations.
If the calculations take more than (say) 2 seconds, I want the loop to give up and move on to the next item (ie. continue).
Is this possible?
How about using microtime to define a break statement?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Then I'd have to include the if statement for microtime in each part of the while loop, surely (in case it gets stuck in a sub-loop)?

I was thinking about set_time_limit - I'll look into that now.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Grim... wrote:Then I'd have to include the if statement for microtime in each part of the while loop, surely (in case it gets stuck in a sub-loop)?

I was thinking about set_time_limit - I'll look into that now.
Is your server running in safemode?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Nope.
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

If you're on unix look at these functions: http://php.net/pcntrl

Hopefully you know how to do multithreaded programming. Have a child process for each calculation. If the script finds out it's a child, set the timeout to 2. Then the mother process loops through and cleans up the zombies if they're dead. I don't know how you would share memory, I you might have to use a database for these guys to store their calculations, or maybe if static variables will carry over, I don't know. You'll have to mess around.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

If you are worried about getting stuck in a loop just set up an iteration counter.

Code: Select all

if(++$i === 1000) break;
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Why not something like...

Code: Select all

function MicroTimeFloat() {
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

$Start = MicroTimeFloat();

while (1 < 2) {
  if (($Start - MicrotimeFloat()) > 2) {
    break;
  }

  // more code here
}
Post Reply