Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Portable IPC::Open3 timeout

by fglock (Vicar)
on Mar 03, 2005 at 00:47 UTC ( #436051=snippet: print w/replies, xml ) Need Help??

Here is a way to timeout Open3 processes without using signals or OS-specific modules.

Tested with Perl 5.8.0 under Windows NT, Perl 5.8.5 under Cygwin, and Perl 5.8.4 under Linux.

    use IPC::Open3;
    use strict;

    my $timeout = 5;
    my ( $wtr, $rdr, $err );

    my $test_program;
    if ( $^O =~ /win32/i )
        $test_program = q{" $|=1; for( 0 .. 10 ) { print \"$_\n\"; sle
+ep 1; } "};
        $test_program = q{ $|=1; for( 0 .. 10 ) { print "$_\n"; sleep 
+1; } };

    my $pid = open3($wtr, $rdr, $err, 'perl', '-e', $test_program );

    my $forked = fork();
    die "fork() failed: $!" unless defined $forked;
    if ( $forked == 0 )
        sleep $timeout;
        kill(9, $pid);

    while ( <$rdr> )
        print $_;
        sleep 1;
    print "finished\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://436051]
[marto]: Wolfsbane , now I'm having flashbacks
[choroba]: Isn't Using PerlPod Creatively rather a meditation?
[choroba]: I don't see a question
[1nickt]: ugh, I stuck my head in the bass bin for 30 seconds on a dare at Ted Nugent at Hammersmith Odeon. Yes, I am 40% deaf now.
[johngg]: My daughter is incredibly jealous of my wife who got to see The Clash at Brixton many years ago. They went to see The Vaccines there recently.
[1nickt]: But the bands are even louder! I saw Spearhead (Michael Franti) at an outdoor show and had to walk a mile away to not feel pain in my chest! Babies were crying ... I asked the sound engineer why it was necessary to have the bass so loud and he laughed...
[Discipulus]: but the best i attended live was Mano Negra Patchanka at Forte Prenestino .. in 1990

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-03-24 12:15 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.