Now these two functions can be executed in parallel way. Meaning that the output of Function_1 doesn't depend on Function_2 or vice versa. The problem is that these functions require a very time consuming process. That's why I need it to run in parallel.Total Sum = Function_1 + Function_2
Which yields: 73.33333333333333#!/usr/bin/perl -w use strict; sub Function_1 { my $up_bound1 = shift; my $total_f1; foreach my $in (0..$up_bound1) { $total_f1 += $in/3; } return $total_f1; } sub Function_2 { my $up_bound2 = shift; my $total_f2; foreach my $in (0 .. $up_bound2) { $total_f2 += $in; } return $total_f2; } ## Begin main process my $up_bound = 10; my $sum = Function_1($up_bound)+Function_2($up_bound); print "$sum\n";
Thus, I humbly seek for enlightment from my fellow monks.#!/usr/bin/perl use warnings; use strict; use POE; my $up_bound = 10; my $final_result = parallel_with_poe($up_bound); sub parallel_with_poe { $ubound = shift; POE::Session->create( inline_states => { # How can I pass the parameter correctly here? _start => Function_1($_KERNEL,$ubound); to_wait => sub { $_[KERNEL]->(delay (tick=>2)); } run_function_2 => Function_2($_KERNEL,$ubound); # This ain't right also, I don't know # how to pass the above result into the function _stop => final_sum($_KERNEL,$ubound); }, ); $poe_kernel->run(); return; # how to return the final value? #exit(0); } sub final_sum { my ($kernel,$ans_of_f1, $ans_of_f2) = @_; my $sum = $ans_of_f1+$ans_of_f2; return $sum; } sub Function_1 { my ($kernel_f1, $up_bound1) = @_; my $total_f1; foreach my $in ( 0 .. $up_bound1) { $total_f1 += $in/3; } return $total_f1; } sub Function_2 { my ($kernel_2,$up_bound2) = @_; my $total_f2; foreach my $in (0 .. $up_bound2) { $total_f2 += $in; } return $total_f2;
In reply to My First POE - Simple Parallel Programming by monkfan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |