Below you can find a program to get you started (read the comments).

It uses POE::Wheel::Run which means that if forks your functions like they would be some programs, collects their STDOUT and the sums it up.

Please note that TIMTOWTDI applies to POE as well ;-) so there are other ways to achieve the same purpose. i.e. look into POE::Filter::Reference for passing data between processes etc.

use strict; use warnings; use POE qw(Wheel::Run); POE::Session->create ( inline_states => { _start => \&start, stdout => \&stdout, done => \&done, }, heap => { sum => 0 } # here your sum will endup ); POE::Kernel->run(); exit; sub start{ my ( $kernel, $heap ) = @_[KERNEL, HEAP]; # If you have more functions with similar interface, just create a + loop my $function = POE::Wheel::Run->new( Program => sub { Function_1( 10 ) }, StdoutEvent => 'stdout', CloseEvent => 'done', ); $heap->{function}->{ $function->ID } = $function; $function = POE::Wheel::Run->new( Program => sub { Function_2( 10 ) }, StdoutEvent => 'stdout', CloseEvent => 'done', ); # store the wheel, so that its refcount is incremented $heap->{function}->{ $function->ID } = $function; } sub stdout { my ($heap, $result ) = @_[HEAP, ARG0]; $heap->{sum}+=$result; } sub done{ my ( $kernel, $heap, $function_id ) = @_[ KERNEL, HEAP, ARG0 ]; # delete the reference to the function that has ended, so it may b +e garbage # collected delete $heap->{function}->{$function_id}; # No more childs, print the total amount # alternately you could send a message back to a parent session wi +th the # result if ( scalar( keys( %{$heap->{function}})) == 0 ){ print "RESULT: ", $heap->{sum},"\n"; } } # The STDOUT of your function is "caught" and returned sub Function_1 { my $up_bound1 = shift; my $total_f1; foreach my $in (0..$up_bound1) { $total_f1 += $in/3; } print "$total_f1\n"; return $total_f1; } sub Function_2 { my $up_bound2 = shift; my $total_f2; foreach my $in (0 .. $up_bound2) { $total_f2 += $in; } print "$total_f2\n"; return $total_f2; }

Dodge This!

In reply to Re: My First POE - Simple Parallel Programming by Ultra
in thread My First POE - Simple Parallel Programming by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.