On 7/28/05, Arijit Das <[EMAIL PROTECTED]> wrote:
> How can I time out a subroutine/function?
>
> print "My code is executing...the next sub inokation
> takes a long time simetimes. SO, I want to ensure that
> at max it should take 5 secsonds.";
> my $device_name = Quota::getqcarg($path);
>
>
> I want to timeout Quota::getqcarg($path) but I don't
> want to use the $SIG{ALRM} technique because I am
> using a older version of Perl which doesn't support
> safe/defferred signals.
>
> Is there any other technique by which I can achive
> this timeout?
>
> Thanks,
> Arijit
something like
my $device_name;
GET_DEVICE_NAME: {
my $GQCpid = open GQC, "-|";
defined($GQCpid) or die "could not fork: $!";
if($GQCpid){
# we are in parent process
my $count = 0;
while ($count++ < 50){
# check for readability on GQC using select() with a 100 ms timeout
...
next unless (some expression indicating readability);
$device_name = <GQC>;
last GET_DEVICE_NAME;
}
$device_name = 'SORRY, TIMEOUT';
kill 9, $GQCpid; # clean up zombies somewhere else or ignore them
}else{
print STDOUT Quota::getqcarg($path);
exit;
};
};
I have not tested this technique and it might make more sense
in a subroutine, to exit with C<return> rather than in a named block
that can be exited with C<last>.
See also MJD's async module on CPAN, that provides some sugar for
such stuff. I'm not sure how it handles timeouts.
--
David L Nicol
"This has been your one free extra mile"
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs