[Gllug] Timeouts in perl

salsaman at xs4all.nl salsaman at xs4all.nl
Tue Oct 31 17:13:43 UTC 2006


- Tethys wrote:

> I'm getting increasingly frustrated with the braindead mess of
> rebarbative syntax that is perl. However, since I know many of you use
> it, perhaps one of you can come to my rescue. I need to time out long
> running operations. Rather than explicitly code a timeout in each
> function (and there are many), I'm trying to write a generic timeout
> wrapper. It goes something like this:
>
> sub sigalrm_handler
> {
>    our $timed_out = 1;
>    die "timeout";
> }
>
> sub timeout
> {
>    my ($timeout, $func, $arg) = @_;
>
>    our $timed_out = 0;
>    eval
>    {
>        local $SIG{ALRM} = \&sigalrm_handler;
>        alarm $timeoutsecs;
>
>        $func -> ($arg);
>        alarm 0;
>    };
>    alarm 0;
>    if ($timed_out == 1) { print "timeout occured\n"; }
> }
>
> The handler is definitely being called (verified by adding a printf to
> it). However, the exception triggered by the die call is not being
> caught by the eval. Do I need to do something explicit to propagate
> the exception up the calling stack? It works if the code is inside the
> eval itself, rather than in a separate function called from within the
> eval. But that tends to default the whole point of the exercise. I
> can't find anything about it in the camel book.
>
> TIA,
>
> Tet

I don't know the answer, but do you need to actually return from the
timeout ? I do something similar, but I just put "&cleanup; exit
$timed_out;" in my alarm function equivalent.

You can then just check the exit value of the program.

Gabriel.


-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list