Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to timeout if a call to an external program takes too long

by deivakumar (Initiate)
on Aug 30, 2007 at 01:42 UTC ( [id://635971]=perlquestion: print w/replies, xml ) Need Help??

deivakumar has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to timeout if a call to an external program takes too long
by BrowserUk (Patriarch) on Aug 30, 2007 at 01:54 UTC

      That is just a perfect solution for me.

      Thanks a bunch to all who responded.

Re: How to timeout if a call to an external program takes too long
by kyle (Abbot) on Aug 30, 2007 at 02:07 UTC

    Can we see the non-working code that uses alarm? This is a textbook example of when to use alarm.

    Here, the alarm is triggered, and then processing continues:

    alarm 5; $SIG{ALRM} = sub { print "alarm!\n" }; my $yawn = `sleep 10 ; echo "yawn"`; print "yawn? $yawn\n"; __END__ alarm! yawn? yawn

    In this case (adapted from the example in the alarm documentation), the alarm is triggered, and processing is halted:

    my $yawn; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 5; $yawn = `sleep 10 ; echo "yawn"`; alarm 0; }; print $@ ? 'Timed out.' : 'Finished.'; print "\n"; print "yawn? $yawn\n"; __END__ Timed out. yawn?

      Have you tried that when safe signals are in force?

      It certainly doesn't work on the OPs platform (win32 going by his use of MyProgram.exe).

      I believe that it also doesn't work on *nix platforms since 5.8.0 unless you explicitly disable safe signals .


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I tried both of my code snippets before posting. My platform is Perl 5.8 on OS X:

        kyle@sulaco:~ $ perl -v | head -6 This is perl, v5.8.6 built for darwin-thread-multi-2level (with 3 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall kyle@sulaco:~ $ uname -a Darwin sulaco 8.10.0 Darwin Kernel Version 8.10.0: Wed May 23 16:50:59 + PDT 2007; root:xnu-792.21.3~1/RELEASE_PPC Power Macintosh powerpc

        I did nothing to disable safe signals. The only part of what I actually executed that I left out of what I posted was my usual "use strict;use warnings;" (and I also cut out the "Use of uninitialized value..." warning that resulted).

        I don't have a Windows machine to test on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://635971]
Approved by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2026-03-10 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.