Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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]: oddly the last time I was there I saw a zeppelin in flight for the first time
[Corion]: ambrus: I don't know how far the budget estimate has been overrun yet
[Corion]: marto: Heh ;) No, you were likely at Tegel or Schönefeld, the new one ("BER") will be somewhat outside of Berlin ;)
[marto]: Corion Schoenfeld, but my friend has been telling me about this new airport for about 7-8 years now :P
[marto]: I doubt it even exists as a building site :P
[ambrus]: Corion: does building airports "somewhat outside of" a city ever work? cities always grow around the airport quickly, because it's practical to be close to it, and after that, people start to complain that the airplanes land so close to their house and sh
[ambrus]: ould keep down the noise

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-02-23 12:01 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (346 votes). Check out past polls.