I think Wheel Run is one of the best parts of POE for what I do!
Especially useful can be the coderef executions. With this example, you
should be able to do any of the following:
# in some routine in my session ...
$kernel->yield( 'run_something_async', "/usr/sbin/sendmail" );
# or
$kernel->yield( 'run_something_async', sub { print "I'm running in child
process $$ ! } );
# or
$kernel->yield( 'run_something_async' [ '/usr/local/bin/my_program',
'-a', $argument_1, '-b', $argument2 ] );
sub run_something_async {
my( $heap, $kernel, $what ) = @_[ HEAP, KERNEL, ARG0 ];
# this isn't necessary if you put the event in inline_states
# at POE::Session creation time
$kernel->state('my_input_event', \&handle_input_event );
$kernel->state('whine_about_error', \&error_handler );
# create a new wheel, storing a reference to it in my heap
$heap->{run_wheel} = POE::Wheel::Run->new(
Program => $what,
ErrorEvent => 'whine_about_error',
StdoutEvent => 'my_input_event',
StderrEvent => 'my_input_event', # or its own handler
Filter => POE::Filter::Line->new()
);
# send SOME DATA to the child process's STDIN
$heap->{run_wheel}->put( "SOME DATA" );
}
# this is input from the child process
sub handle_input_event {
my( $heap, $buffer, $wheel_id ) = @_[ HEAP, ARG0, ARG1 ];
print "I got this: '$buffer' from wheel $wheel_id\n";
# Send SOME MORE DATA to the child's standard in
$heap->{run_wheel}->put( "SOME MORE DATA" );
}
sub error_handler {
my($heap,$buffer,$error,$wheel_id) = @_[HEAP,ARG0,ARG1,ARG2];
# do something ...
}
-Al Tobey
Unix Administrator
On Thu, 2002-03-14 at 17:01, Ryan White wrote:
> I have tried a number of different things. Immediately after I sent my post
> I noticed the missing LF. I have added that and I still get no return value.
> Inline comments...
>
> From: "Rocco Caputo" <[EMAIL PROTECTED]>
> > On Thu, Mar 14, 2002 at 01:45:28AM -0800, Ryan White wrote:
> > > I am looking for an example using Wheel::Run within a child process. I
> need
> > > to know how to read the output of a Wheel after using put.
> [..]
> > >
> > > sub child_start {
> > > my ($kernel, $session, $parent, $heap, $email) = @_[KERNEL, SESSION,
> > > SENDER, HEAP, ARG0];
> > >
> > > # Remember the parent.
> > > $heap->{parent} = $parent;
> > >
> > > warn "Child ", $session->ID, " will validate $email.\n";
> > >
> > > # Check the passed email address.
> > >
> > > my $program = [ "./check_addr.pl" ];
> > > my $wheel = POE::Wheel::Run->new(
> > > Program => $program,
> > > StdoutEvent => &stdout, # Event to emit with child
> stdout
>
> I have also tried using 'stdout' but it never calls the stdout method..
> Where should the stdout method be defined. I had the following in my creat
> child function:
> sub create_a_child {
> my $email = shift;
> POE::Session->create
> ( inline_states =>
> { _start => \&child_start,
> _stop => \&child_stop,
> stdout => \&child_stdout,
> },
> args => [$email],
> );
> }
>
> Is this not the correct method? When I used this code it would never call te
> child_stdout method. Which session does it need to be in?
>
> [..]
> > > }
> >
> > I see two problems here:
> >
> > First, you are not storing the new POE::Wheel::Run instance in $heap,
> > so it's destructing as soon as child_start() returns.
>
> I didn't see anything in the POE::Wheel::Run docs about putting the instance
> in $heap. What is the convention for that
>
> > Second, your StdoutEvent should be an event name, not a code
> > reference.
> >
> > [...]
> >
> > > And the check_addr.pl program simply looks like this
> > > #!/usr/bin/perl
> > > my $email = <>;
> > > chomp($email);
> > > print 1;
> > >
> > > It doesn't seem to ever be able to read the data that check_addr sends
> back.
> > > Should I be doing this another way?
>
> Ok the script now looks like this
> #!/usr/bin/perl
> use strict;
> $| = 1;
> my $email = <>;
> chomp($email);
> print "1\n";
>
> I over simplified the script so that it only test the wheel processes. I am
> attaching the scripts so that you can see if I'm missing something easier.
> What I think it happening (not sure if it make sense) is the child it
> calling the wheel but has nothing else to do so it then exits before getting
> stdout.
>
********************************************************************
This email and any files transmitted with it are confidential
and intended solely for the use of the individual or entity
to whom they are addressed. If you have received this
email in error please notify the Priority Health Information
Services Department at (616) 942-0954.
********************************************************************