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]
LanX is not affraid of Trump ...
LanX ... me is scared of people voting for Trumps
[erix]: until good old Putin lobs a bomb on Berlin :)
[Your Mother]: “I’m afraid of Americans…”
[Your Mother]: “No one needs anyone / They don’t even just pretend…”
[erix]: they've stuck nuclear missiles in Kaliningrad - they can just reach Berlin :)
[LanX]: naaah! Putin wouldn't nuke all the Russians in Berlin
[Your Mother]: Putin is a bad guy but he’s not that kind of bad guy. (Who would launch.)
[erix]: fortunately, Putin seems to be ill -- or at least, they're building a hospital for him and his cronies in Moscow
[erix]: nobody knows what kind it takes :)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-01-20 00:52 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.