On 3/9/2011 6:56 AM, Hannes Landeholm wrote:
A simple way to implement this would be to register a function that would be
called N seconds before the script would timeout.

register_timeout_handler(2, function() { die("PHP timed out."); });

It would be called just as a shutdown function - in fact I'd like to use the
same function as my shutdown function and get the error with
error_get_last(). Of course set_time_limit(0) could be used in this function
to prevent the timeout of the timeout handler. This does not "prevent"
timeout since set_time_limit could have been called by the script before the
timeout anyway.

I like this. Although, I'd rather the first parameter were in milliseconds instead of seconds. This would tell PHP 'x' milliseconds before script termination to call the specified function. That function would then have the remainder of the time slice to execute cleanup routines and send any errors to the user. This approach also doesn't require a whole new configuration option in php.ini.


On that note I also miss a function which returns the time the script can
keep running for. If that calculate needs to be calculated to implemented to
implement this, why not make the value available to the PHP script?

This is already possible to do. I do this in WebCron by setting a variable with the value of microtime() at the start of execution and figuring out how long the script can actually run for. Then, I stop executing a task if there are less than a few seconds left on the clock so that there is ample time to clean up gracefully. For more information:

http://barebonescms.com/documentation/webcron/

Then I expose a convenience function called WC_GetTimeLeft() to modules and tasks that can use it to test to see how much time is left on the clock to execute the script. The approach works quite well. As an example, I use WC_GetTimeLeft() extensively in my WebCron Site Backup module that can backup websites of any size over HTTP/HTTPS.

http://barebonescms.com/documentation/webcron_site_backup/

So, while it can and has been done, it does require a little extra work at the start of the script and obviously isn't as accurate as a dedicated function in PHP itself would be.

--
Thomas Hruska
CubicleSoft President

Barebones CMS is a high-performance, open source content management system for web developers operating in a team environment.

An open source CubicleSoft initiative.
Your choice of a MIT or LGPL license.

http://barebonescms.com/


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to