develooper Front page | perl.beginners | Postings from November 2002

RE: how do i make a script run for a certain period of time?

Thread Previous | Thread Next
From:
david
Date:
November 27, 2002 11:51
Subject:
RE: how do i make a script run for a certain period of time?
Message ID:
20021127195134.8668.qmail@onion.perl.org
Nyimi Jose wrote:

>> -----Original Message-----
>> From: david [mailto:dzhuo@looksmart.net]
>> Sent: Wednesday, November 27, 2002 8:34 PM
>> To: beginners@perl.org
>> Subject: RE: how do i make a script run for a certain period of time?
>> 
>> 
>> Nyimi Jose wrote:
>> 
>> > If you want your loop to first finish
>> > what he is doing before die, do this instead.
>> > Thus,when you receive the alarm sig you exit
>> > first the loop then the script.
>> > 
>> > 
>> > #!/usr/bin/perl -w
>> > 
>> > 
>> 
>> you must be a bad bad guy!!! :-) just kidding!
> 
> Sorry !
> 
>> never listen to the perldoc hua? don't ever mix sleep and
>> alarm! on most platform, sleep is implemented using alarm
>> call. the problem? the
>> alarm call inside the sleep function will thus replace your
>> alarm function
>> just outside the while loop. check the perldoc -f alarm for more.
> 
> What about this ?:
> 
> use strict
> my $time_to_die=0;
> my $timeout=30; #in seconds
> 
> $SIG{ALRM} = sub { $time_to_die=1; };
> 
> alarm($timeout);
> while(!$time_to_die){
> #tasks here
> }
> __END__
> 

much better. a bit different than my first post. your code will at least 
execute the whole block of the while loop. my original post will die in the 
middle. if he never want to die in the middle of the while loop even the 
alarm expires, your solution is better. if he want to die as soon as the 
alarm expires, my solution is bit better.

david

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About