Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 199cb74c34
Fetching contributors…

Cannot retrieve contributors at this time

file 84 lines (66 sloc) 2.056 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#!/usr/bin/perl
# vim: ts=2 sw=2 noexpandtab

# Demonstrate subprocesses without POE::Wheel::Run.
# Test case for upcoming Reflex::Run, which drives IPC::Run.

use warnings;
use strict;
use lib qw(../lib);

use Runner;

my $cmd = [
$^X, '-MTime::HiRes=sleep', '-wle',
q($|=1;) .
q(for (1..3) { $_ = qq[pid($$) moo($_)]; print; warn "$_\n"; sleep rand; })
];

sub my_start {
my $cmd = shift;

use IPC::Run qw(start);
use Symbol qw(gensym);

my ($fh_in, $fh_out, $fh_err) = (gensym(), gensym(), gensym());

my $ipc_run = start(
$cmd,
'<pipe', $fh_in,
'>pipe', $fh_out,
'2>pipe', $fh_err,
) or die "IPC::Run start() failed: $? ($!)";

return($ipc_run, $fh_in, $fh_out, $fh_err);
}

my ($ipc_run_1, $runner_1);
{
($ipc_run_1, my($fh_in, $fh_out, $fh_err)) = my_start($cmd);

$runner_1 = Runner->new(
stdin => $fh_in,
stdout => $fh_out,
stderr => $fh_err,
pid => $ipc_run_1->{KIDS}[0]{PID},

on_stdout_closed => sub { print "runner_1 stdout closed\n" },
on_stderr_closed => sub { print "runner_1 stderr closed\n" },
on_stdout_data => sub { print "runner_1 stdout: ", $_[1]->octets() },
on_stderr_data => sub { print "runner_1 stderr: ", $_[1]->octets() },

on_exit => sub {
my ($self, $child) = @_;
warn "runner_1 child ", $child->pid(), " exited: ", $child->exit(), "\n";
$runner_1 = undef;
},
);
}

my ($ipc_run_2, $runner_2);
{
($ipc_run_2, my($fh_in, $fh_out, $fh_err)) = my_start($cmd);

$runner_2 = Runner->new(
stdin => $fh_in,
stdout => $fh_out,
stderr => $fh_err,
pid => $ipc_run_2->{KIDS}[0]{PID},

on_stdout_closed => sub { print "runner_2 stdout closed\n" },
on_stderr_closed => sub { print "runner_2 stderr closed\n" },
on_stdout_data => sub { print "runner_2 stdout: ", $_[1]->octets() },
on_stderr_data => sub { print "runner_2 stderr: ", $_[1]->octets() },

on_exit => sub {
my ($self, $child) = @_;
warn "runner_2 child ", $child->pid(), " exited: ", $child->exit(), "\n";
$runner_2 = undef;
},
);
}

Reflex->run_all();
exit;
Something went wrong with that request. Please try again.