Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Portable IPC::Open3 timeout

by fglock (Vicar)
on Mar 03, 2005 at 00:47 UTC ( [id://436051]=CUFP: 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; } "}; } else { $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); exit; } while ( <$rdr> ) { print $_; sleep 1; } print "finished\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found