Yes, it's possible to measure the "time left" yourself. However set_time_limit documentation states that not all time are accounted for when measuring how long the script can run for. If it's a UNIX system and you're using system operations/database queries etc there will be a difference between the time you measure the script has run for and the time PHP has measured.
And this is only a suitable solution in controlled code like when you're doing something defined in a loop and can measure the time with regular intervals. I need it for my framework which can contain any kind of logic, even logic that uses set_time_limit by itself. ~Hannes On 9 March 2011 15:52, Thomas Hruska <thru...@cubiclesoft.com> wrote: > 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 > >