Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

POE, Sessons, POE::Wheel::Run and program flow

by Devanchya (Beadle)
on Sep 04, 2008 at 14:06 UTC ( [id://709022]=perlquestion: print w/replies, xml ) Need Help??

Devanchya has asked for the wisdom of the Perl Monks concerning the following question:

This Question has been edited to include code sample

In what I hope is my final question on POE... and thus giving me enough information to finish this mini-project I ask the following:

I have the following structure, and I apologize for no code sample at the moment. This is an extension to my questions: http://perlmonks.org/?node_id=708535 and

I have a POE::Component::JobQueue which I get information off passively. This works great, it goes into the new session and is able to Trigger a POE::Wheel::Run which runs an application that takes about 45 minutes to complete.

The Issue is:
As soon as the new Session with the Wheel::Run in it is fired, the session returns to the JobQueue saying it is finished. This isn't true, it should only be returning finished once the ChildClosed event is done in the Wheel::Run

As is normal, I have searched Google and the POE Cookbooks and still scratch my head.

POE::Component::JobQueue->spawn ( Alias => 'MyQueue', WorkerLimit => $numOfWorkerThreads, Worker => \&make_a_worker, Passive => { }, ); # Code that creates a seperate Session # Snipped due to Vendor limits sub handle_message { $kernel->yield( enqueue_msg => $msg ); } exit 0; # This gets posted to sub enqueue_msg { my ($kernel, $session, $message) = @_[ KERNEL, SESSION, ARG0 ]; print "-enqueue_job: -- Session: ", $session->ID, " message\n"; $kernel->post( MyQueue => 'enqueue', 'finish_job', $message ); } sub make_a_worker { my ( $postback, $message ) = @_; my @results = create_worker_session( $message ); $postback->(@results); } ## Do the actual creation of the worker session sub create_worker_session { my $message = shift; POE::Session->create( inline_states => { _start => \&worker_start, worker_child_stdout => \&worker_child_stdout, worker_closed => \&worker_closed, sig_child => \&sig_child, }, args => [ '', $message ], ); return "Message ID: ".$message->getHeaderField('MessageID')." -- F +inished... \n"; } sub worker_start { my ($kernel, $session, $heap, @args ) = @_[KERNEL, SESSION, HEAP, +ARG0 .. $#_]; $heap->{MessageID} = $args[1]->getHeaderField('MessageID'); $heap->{SessionID} = $session->ID; print " Message ID: ".$heap->{MessageID}; print " -start_message_process: ".$heap->{SessionID}."\n"; my $fullCMD = "/my/long/running/command -i foobar"; $heap->{cmdlinefish} = POE::Wheel::Run->new( Program => $fullCMD, StdoutEvent => "worker_child_stdout", CloseEvent => "worker_closed", StdioFilter => POE::Filter::Reference->new(), ); print " Message ID: ".$heap->{MessageID}; print " -start_message_process (end): ".$heap->{SessionID}."\n"; } sub worker_child_stdout { my ( $heap, $preargs ) = @_[ HEAP, ARG0 ]; print " Message ID: ".$heap->{MessageID}; print " -worker_child_stdout: $preargs\n"; } sub worker_closed { my ( $kernel, $heap, $preargs ) = @_[ KERNEL, HEAP, ARG1 ]; print " Message ID: ".$heap->{MessageID}; print " -worker_closed: $preargs\n"; $kernel->yield('sig_child'); } sub sig_child { my ( $heap, $sig, $pid, $exit_val ) = @_[ HEAP, ARG0, ARG1, ARG2 ] +; my $details = delete $heap->{cmdlinefish}; print " Message ID: ".$heap->{MessageID}; print " -sig_child\n"; return 0; }
Even smart people are dumb in most things...

Replies are listed 'Best First'.
Re: POE, Sessons, POE::Wheel::Run and program flow
by themage (Friar) on Sep 04, 2008 at 17:22 UTC
    sub make_a_worker { my ( $postback, $message ) = @_; POE::Session->create( inline_states => { _start => \&worker_start, worker_child_stdout => \&worker_child_stdout, worker_closed => \&worker_closed, sig_child => \&sig_child, }, args => [ @_ ], ); } sub worker_start { my ($kernel, $session, $heap, @args ) = @_[KERNEL, SESSION, HEAP, +ARG0 .. $#_]; $heap->{postback}=$args[0]; $heap->{message}=$args[1]; $heap->{MessageID} = $args[1]->getHeaderField('MessageID'); $heap->{SessionID} = $session->ID; # ... your code as is... print " Message ID: ".$heap->{MessageID}; print " -start_message_process (end): ".$heap->{SessionID}."\n"; } # .... sub worker_closed { my ( $kernel, $heap) = @_[ KERNEL, HEAP]; my $postback=$heap->{postback}; my @results=@{$heap->{results}}; # I don't know how $heap->{results} is created, but I presume it i +s based somehow on the output of your process $postback->(@results); # ... anything you need (yield('sig_child') is not one of that) }


    This should do it.

    Your problems, create a session (and that include almost any type of component don't ways for the session to end, one create it and add it to the sessions poll, so when you call your callback after create the session you are signaling the JobQueue that the job is done.

      That did it! Thanks.
      Even smart people are dumb in most things...
Re: POE, Sessons, POE::Wheel::Run and program flow
by rhesa (Vicar) on Sep 04, 2008 at 15:19 UTC
    As soon as the new Session with the Wheel::Run in it is fired, the session returns to the JobQueue saying it is finished. This isn't true, it should only be returning finished once the ChildClosed event is done in the Wheel::Run.
    That shouldn't be happening. The job should be signaled as being finished only after calling the postback callback.

    It's kinda hard to tell exactly how your program flow is without seeing the actual code. The code snippets in your previous thread don't give me enough of an idea to tell where it's going wrong. The only thing I notice is that you do not use the $postback anywhere in your worker session. Maybe that's it.

    Try stuffing the $postback into the session's heap, and call it when you receive the ChildClosed event, or perhaps in the session's _end handler.

Re: POE, Sessons, POE::Wheel::Run and program flow
by themage (Friar) on Sep 04, 2008 at 15:14 UTC
Re: POE, Sessons, POE::Wheel::Run and program flow
by Devanchya (Beadle) on Sep 04, 2008 at 16:20 UTC
    I have posted the code sample now. I did not have something I could post earlier that could be public. This is basically a couple of cookbooks put together.
    Even smart people are dumb in most things...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-28 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found