Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51638 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11805 invoked from network); 9 Mar 2011 15:04:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Mar 2011 15:04:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=landeholm@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=landeholm@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: landeholm@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-iw0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:35178] helo=mail-iw0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FA/05-21312-9E6977D4 for ; Wed, 09 Mar 2011 10:04:12 -0500 Received: by iwn3 with SMTP id 3so624324iwn.29 for ; Wed, 09 Mar 2011 07:04:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=IRCnPhdqqPHN/86K0JZvp/eTYlk7Hl5dSNVxhJdA2lY=; b=Lgpal4RQBT6dyb7JFPItlxeBvF9U9hjSrRbVh5udB9bXZcc17CaR8dWp54MZSCRL4Q TNjiU4yFepibEWnPq9yXS3d5sZQOkfwJfImAu68JfhXiL96pUubvbtLnyNd3qeZjswkb DUMSgL+7F35D8NF53byBCC6MtZZCxzvvNm7sE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=Bhk8dAJhn/KJ1aaw08sctC+KZ4ljDrDDodi2TJTvrjvm5YHlcQFZSJZiC6kpmiwEJm JehXCBkUc0s7FyfztwZBSMiRw0Et5mHD9ozzwWt2YZV65yAuy93xWVgIDE3kC6zvMEg/ 1HejELYej7Q6rVyk4VOzR/2JdyWp1cEyDJMW8= MIME-Version: 1.0 Received: by 10.42.196.9 with SMTP id ee9mr7774056icb.21.1299683026901; Wed, 09 Mar 2011 07:03:46 -0800 (PST) Received: by 10.231.17.68 with HTTP; Wed, 9 Mar 2011 07:03:46 -0800 (PST) In-Reply-To: <4D77942F.9020704@cubiclesoft.com> References: <4D7629B5.4090007@php.net> <4D76D849.6000103@gmail.com> <4D77942F.9020704@cubiclesoft.com> Date: Wed, 9 Mar 2011 16:03:46 +0100 Message-ID: To: Thomas Hruska , internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf303bfffc239df5049e0e09e4 Subject: Re: [PHP-DEV] Make set_time_limit() timeout a catchable fatal error From: landeholm@gmail.com (Hannes Landeholm) --20cf303bfffc239df5049e0e09e4 Content-Type: text/plain; charset=ISO-8859-1 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 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 > > --20cf303bfffc239df5049e0e09e4--