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

How to handle PHP execution timeouts gracefully?

P: n/a
We have developed an open-source eCommerce sales forecasting add-on in
PHP (see link below) that requires usually more than a few seconds to
complete because the process involves some Web Services
communications.
http://community.lokad.com/PhpSalesForecasting.ashx

Yet, in shared hosting with short PHP execution timeouts, our script
may not terminate (being killed by the web server).

Our concern is not the timeout in itself, it's the fact that this
situation is very confusing for the user who may not even be aware
that a timeout has been encountered and who may experience random bugs
due to this timeout.

Thus we need to handle gracefully the PHP execution timeouts by
displaying appropriate and meaningful error messages.

Do anyone knows how to do that?

Thanks in advance,
Joannès
http://www.lokad.com, sales forecasting

Mar 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Joannes Vermorel" <jo**************@gmail.comwrote:
We have developed an open-source eCommerce sales forecasting add-on in
PHP (see link below) that requires usually more than a few seconds to
complete because the process involves some Web Services
communications.
http://community.lokad.com/PhpSalesForecasting.ashx

Yet, in shared hosting with short PHP execution timeouts, our script
may not terminate (being killed by the web server).

Our concern is not the timeout in itself, it's the fact that this
situation is very confusing for the user who may not even be aware
that a timeout has been encountered and who may experience random bugs
due to this timeout.

Thus we need to handle gracefully the PHP execution timeouts by
displaying appropriate and meaningful error messages.

Do anyone knows how to do that?
Two solutions:

1) You said that some other web services are involved, so your program acts
as a sort of gateway between the remote client and those services. Maybe the
504 error "Gateway timeout" of the HTTP protocol is appropriate in this case.

2) Using declare(), see http://www.php.net/manual/en/control...es.declare.php
For example:

define("MAX_EXECUTION_TIME", 10); # seconds

$timeline = time() + MAX_EXECUTION_TIME;

function check_timeout()
{
if( time() < $GLOBALS['timeline'] )
return;

# timeout reached:
echo "<html><html>Sorry, we are very busy, retry later!</body></html>";
exit;
}

register_tick_function("check_timeout");

declare( ticks=1 ){
# here the process that might require
# so much time; any output generated is
# keep inside a string $s
}

# Ok, process completed, output the result:
echo $s;
Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

Mar 21 '07 #2

P: n/a
Thank you for your answer. We will what we can do with the declare()
method that you're suggesting.

Joannes
http://www.lokad.com

Mar 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.