Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51636 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4914 invoked from network); 9 Mar 2011 14:52:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Mar 2011 14:52:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=thruska@cubiclesoft.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=thruska@cubiclesoft.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain cubiclesoft.com designates 74.208.167.7 as permitted sender) X-PHP-List-Original-Sender: thruska@cubiclesoft.com X-Host-Fingerprint: 74.208.167.7 u15187375.onlinehome-server.com Windows 2000 SP4, XP SP1 Received: from [74.208.167.7] ([74.208.167.7:3455] helo=mail.cubiclesoft.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/93-21312-934977D4 for ; Wed, 09 Mar 2011 09:52:42 -0500 Received: from [192.168.2.100] ([174.18.42.80]) by cubiclesoft.com with MailEnable ESMTP; Wed, 9 Mar 2011 09:52:37 -0500 Message-ID: <4D77942F.9020704@cubiclesoft.com> Date: Wed, 09 Mar 2011 07:52:31 -0700 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: PHP Development References: <4D7629B5.4090007@php.net> <4D76D849.6000103@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Make set_time_limit() timeout a catchable fatal error From: thruska@cubiclesoft.com (Thomas Hruska) 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/