By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Long sleep() and time-out's

P: n/a
Hi

The script I'm writing needs to wait a random amount of time (1-300s)
between each iteration of a loop, eg.

while(condition) {
do something
//wait between 1s and 5 minutes
sleep(rand(1,300));
}

Problem is, a browser times out if it doesn't get an answer within
about 30 seconds. Is the idea of having a PHP script sleep for a
longer amount of time incompatible with web applications? Any
work-around?

Thank you.
Jun 14 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Rik
Vincent Delporte wrote:
Hi

The script I'm writing needs to wait a random amount of time (1-300s)
between each iteration of a loop, eg.

while(condition) {
do something
//wait between 1s and 5 minutes
sleep(rand(1,300));
}

Problem is, a browser times out if it doesn't get an answer within
about 30 seconds. Is the idea of having a PHP script sleep for a
longer amount of time incompatible with web applications? Any
work-around?


Feed some bogus information?

for($i=0;$i<=rand(1,60);$i++){
echo ' ';
sleep(5);
}

Not very elegant, but gets the job done I think.

Grtz,
--
Rik Wasmus
Jun 14 '06 #2

P: n/a
Vincent Delporte wrote:
Hi

The script I'm writing needs to wait a random amount of time (1-300s)
between each iteration of a loop, eg.

while(condition) {
do something
//wait between 1s and 5 minutes
sleep(rand(1,300));
}

Problem is, a browser times out if it doesn't get an answer within
about 30 seconds. Is the idea of having a PHP script sleep for a
longer amount of time incompatible with web applications? Any
work-around?
Use set_time_limit:
http://php.net/set_time_limit

From the manual: Set the number of seconds a script is allowed to run. If this is
reached, the script returns a fatal error. The default limit is 30
seconds or, if it exists, the max_execution_time value defined in the
php.ini. If seconds is set to zero, no time limit is imposed.

When called, set_time_limit() restarts the timeout counter from zero.
In other words, if the timeout is the default 30 seconds, and 25
seconds into script execution a call such as set_time_limit(20) is
made, the script will run for a total of 45 seconds before timing out.


Especially that last part should be useful to you.

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jun 14 '06 #3

P: n/a
Rik
> Vincent Delporte wrote:
Problem is, a browser times out if it doesn't get an answer within
-----------------^^^^^^

Kim André Akerĝ wrote: Use set_time_limit:
http://php.net/set_time_limit


Nope, except if the OP has got his question wrong.
Browser time-outs are beyond settings you can control, you can only
urge/fool them.

Grtz
--
Rik Wasmus
Jun 14 '06 #4

P: n/a
On Wed, 14 Jun 2006 18:19:45 +0200, "Rik" <lu************@hotmail.com>
wrote:
for($i=0;$i<=rand(1,60);$i++){
echo ' ';
sleep(5);
}

Not very elegant, but gets the job done I think.


Thx but it doesn't work: I don't see how I can have the script pause
for 1-300 seconds, while still send some empty string to the browser
every 5 seconds just to avoid a time-out error. Did I get this wrong?
Jun 14 '06 #5

P: n/a
Rik
Vincent Delporte wrote:
On Wed, 14 Jun 2006 18:19:45 +0200, "Rik" <lu************@hotmail.com>
wrote:
for($i=0;$i<=rand(1,60);$i++){
echo ' ';
sleep(5);
}

Not very elegant, but gets the job done I think.


Thx but it doesn't work: I don't see how I can have the script pause
for 1-300 seconds, while still send some empty string to the browser
every 5 seconds just to avoid a time-out error. Did I get this wrong?


It depends.
What is the exact reason of your pause?

Grtz,
--
Rik Wasmus
Jun 14 '06 #6

P: n/a
On Wed, 14 Jun 2006 21:40:08 +0200, "Rik" <lu************@hotmail.com>
wrote:
What is the exact reason of your pause?


I have a main loop that fetches information from another site, but to
simulate fake users, I need these fetching to occur at random
intervals, between 1 and 300s. The problem is that after any sleep >
30 seconds, the browser reports a 504 (time-out).
Jun 14 '06 #7

P: n/a
Vincent Delporte wrote:
On Wed, 14 Jun 2006 21:40:08 +0200, "Rik" <lu************@hotmail.com>
wrote:
What is the exact reason of your pause?

I have a main loop that fetches information from another site, but to
simulate fake users, I need these fetching to occur at random
intervals, between 1 and 300s. The problem is that after any sleep >
30 seconds, the browser reports a 504 (time-out).


A 504 sounds like the browser timing out (although I guess it could be PHP).

As has been indicated before, you can change the PHP timeout value, but there's
nothing you can do about the browser.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 15 '06 #8

P: n/a
On Thu, 15 Jun 2006 00:01:42 -0400, Jerry Stuckle
<js*******@attglobal.net> wrote:
As has been indicated before, you can change the PHP timeout value, but there's
nothing you can do about the browser.


OK, so it's hopeless. It's a shared PHP server to which I don't have
access, so no fiddling with PHP.INI. I'll rewrite this in Delphi
instead.

Thanks everyone.
Jun 15 '06 #9

P: n/a
Vincent Delporte wrote:
On Thu, 15 Jun 2006 00:01:42 -0400, Jerry Stuckle
<js*******@attglobal.net> wrote:
As has been indicated before, you can change the PHP timeout value, but there's
nothing you can do about the browser.

OK, so it's hopeless. It's a shared PHP server to which I don't have
access, so no fiddling with PHP.INI. I'll rewrite this in Delphi
instead.

Thanks everyone.


If it's a timeout problem in the browser it doesn't matter what language you
use. The problem isn't in anything you can control.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 16 '06 #10

P: n/a
On Thu, 15 Jun 2006 23:51:18 -0400, Jerry Stuckle
<js*******@attglobal.net> wrote:
If it's a timeout problem in the browser it doesn't matter what language you
use. The problem isn't in anything you can control.


I meant that I would rewrite the algo in Delphi instead of a PHP
script. That will solve the sleep(rand()) issue. Thanks.
Jun 16 '06 #11

P: n/a
On Fri, 16 Jun 2006 07:08:31 +0200, Vincent Delporte wrote:
If it's a timeout problem in the browser it doesn't matter what language
you use. The problem isn't in anything you can control.


I meant that I would rewrite the algo in Delphi instead of a PHP script.
That will solve the sleep(rand()) issue. Thanks.


Unless I'm missing something Jerry's answer still stands. If you write a
server-side CGI exe using Delphi, the browser will still timeout while
waiting for your random wait.

If you are planning on running the Delphi utility through some other
method (at, scheduled jobs, etc) then you could do the same thing with PHP
(using set_time_limit()) and it would work equally fine.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 16 '06 #12

P: n/a
Andy Jeffries wrote:
On Fri, 16 Jun 2006 07:08:31 +0200, Vincent Delporte wrote:
If it's a timeout problem in the browser it doesn't matter what language
you use. The problem isn't in anything you can control.

I meant that I would rewrite the algo in Delphi instead of a PHP script.
That will solve the sleep(rand()) issue. Thanks.


Unless I'm missing something Jerry's answer still stands. If you write a
server-side CGI exe using Delphi, the browser will still timeout while
waiting for your random wait.

If you are planning on running the Delphi utility through some other
method (at, scheduled jobs, etc) then you could do the same thing with PHP
(using set_time_limit()) and it would work equally fine.

Cheers,
Andy

You could always respond to the browser before you go into the sleep
loop and redirect it to a 'waiting...' page with a meta-refresh on it.
The waiting page can check whether the fetch from the remote is complete
and either a) display a new page with the details or b) call itself and
loop again.

Maybe a c) if we have looped too many times to say 'the data isn't coming'.

-david-

Jun 16 '06 #13

P: n/a
On Fri, 16 Jun 2006 20:13:37 GMT, Andy Jeffries
<ne**@andyjeffries.co.uk> wrote:
Unless I'm missing something Jerry's answer still stands. If you write a
server-side CGI exe using Delphi, the browser will still timeout while
waiting for your random wait.


No, I meant rewriting this as a Delphi client app instead of a PHP
script. Too bad that the whole thing breaks because I didn't think
about the sleep() thing. No biggie though. Thanks everyone for your
help.
Jun 16 '06 #14

P: n/a
On Fri, 16 Jun 2006 16:20:42 -0400, David Haynes
<da***********@sympatico.ca> wrote:
You could always respond to the browser before you go into the sleep
loop and redirect it to a 'waiting...' page with a meta-refresh on it.
The waiting page can check whether the fetch from the remote is complete
and either a) display a new page with the details or b) call itself and
loop again.

Maybe a c) if we have looped too many times to say 'the data isn't coming'.


Thanks a bunch for the idea, but it's just too complicated. I'll be
done in Delphi this week-end :-)
Jun 16 '06 #15

P: n/a
Vincent Delporte wrote:
Hi

The script I'm writing needs to wait a random amount of time (1-300s)
between each iteration of a loop, eg.

while(condition) {
do something
//wait between 1s and 5 minutes
sleep(rand(1,300));
}

<snip>

Not sure, if you're looking for
<http://in2.php.net/ignore_user_abort> and
<http://in2.php.net/features.connection-handling>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 18 '06 #16

P: n/a
On 18 Jun 2006 03:58:18 -0700, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> wrote:
Not sure, if you're looking for
<http://in2.php.net/ignore_user_abort> and
<http://in2.php.net/features.connection-handling>


Thanks, but it seems like the shared server I use ignores those
settings.
Jun 18 '06 #17

P: n/a
On Fri, 16 Jun 2006 23:07:45 +0200, Vincent Delporte wrote:
Unless I'm missing something Jerry's answer still stands. If you write a
server-side CGI exe using Delphi, the browser will still timeout while
waiting for your random wait.


No, I meant rewriting this as a Delphi client app instead of a PHP script.
Too bad that the whole thing breaks because I didn't think about the
sleep() thing. No biggie though. Thanks everyone for your help.


You could have done it as a PHP script and just run it on your local
machine (as you were doing with Delphi) using the PHP binary that is
installed with newer versions of PHP.

I know how you feel though (exactly, I used to be a Delphi programmer) but
it really will do you great bounds to resist the temptation to go back to
the old familiar when learning a new language.

It's hard, I'm as guilty as anyone else, but it really does you good.

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 18 '06 #18

P: n/a
On Sun, 18 Jun 2006 21:31:51 GMT, Andy Jeffries
<ne**@andyjeffries.co.uk> wrote:
You could have done it as a PHP script and just run it on your local
machine (as you were doing with Delphi) using the PHP binary that is
installed with newer versions of PHP.


Mmm... Maybe I'll do that, although i'd rather run it from the coloc
server. Either way, it's good training with regexes :-)

Thx.
Jun 19 '06 #19

P: n/a
Vincent Delporte wrote:
On 18 Jun 2006 03:58:18 -0700, "R. Rajesh Jeba Anbiah"
<ng**********@rediffmail.com> wrote:
Not sure, if you're looking for
<http://in2.php.net/ignore_user_abort> and
<http://in2.php.net/features.connection-handling>


Thanks, but it seems like the shared server I use ignores those
settings.


I would suggest you to check the links.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 19 '06 #20

P: n/a
On Mon, 19 Jun 2006 03:00:29 +0200, Vincent Delporte wrote:
You could have done it as a PHP script and just run it on your local
machine (as you were doing with Delphi) using the PHP binary that is
installed with newer versions of PHP.


Mmm... Maybe I'll do that, although i'd rather run it from the coloc
server. Either way, it's good training with regexes :-)


Sorry, I obviously haven't been very clear... My point was if you write it
in Delphi as a CGI-EXE handler and connect to it with a browser, then
you'll get the timeouts in exactly the same way (as it's the browser
timing out). If you run the Delphi EXE locally (or on the server using
CRON/AT/scheduling) then you can do exactly the same thing with PHP using
the command line binary for PHP and achieve perfect results.

My point is, redoing it in Delphi gives you nothing (except a Windows
interface if you want that).

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 19 '06 #21

P: n/a
On Mon, 19 Jun 2006 07:30:47 GMT, Andy Jeffries
<ne**@andyjeffries.co.uk> wrote:
My point is, redoing it in Delphi gives you nothing (except a Windows
interface if you want that).


I know, but since I was only getting started, it's no big deal. I'll
just keep in mind that PHP and long sleep()'s aren't a good idea.

Cheers.
Jun 19 '06 #22

P: n/a
On Mon, 19 Jun 2006 21:51:15 +0200, Joe DiMaggio wrote:
My point is, redoing it in Delphi gives you nothing (except a Windows
interface if you want that).


I know, but since I was only getting started, it's no big deal. I'll just
keep in mind that PHP and long sleep()'s aren't a good idea.


*when used in a browser-server context*

Using a PHP CLI binary, it's fine.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jun 20 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.