Hi Microcebus,

You were close. Notice the arguments given to $pm->start and $pm->finish. Start accepts an optional identifier for the process. That same identifier is used as the 3rd argument for the on_finish callback. In the demonstration that follows, I puposely have read_GTF die with an exception. The output is the same whether threads equals 1 or 4.

Like afoken mentioned here, Parallel::ForkManager must be 0.7.6 or later for the demonstration to run.

use strict; use warnings; use feature 'say'; use Parallel::ForkManager; my $threads = 4; my @name = ('read_genome', 'read_mapfile', 'read_GTF', 'read_RM'); my @task = (\&read_genome, \&read_mapfile, \&read_GTF, \&read_RM); my (@ret, @err); if ($threads == 1) { for my $id (0 .. $#task) { $ret[$id] = eval { $task[$id]->() }; $err[$id] = $@; } } else { my $pm = new Parallel::ForkManager($threads); $pm->set_waitpid_blocking_sleep(0); $pm->run_on_finish( sub { my ($pid, $exit, $id, $signal, $core, $data) = @_; $ret[$id] = delete $data->{ret}; $err[$id] = delete $data->{err}; }); for my $id (0 .. $#task) { $pm->start($id) and next; my $res = eval { $task[$id]->() }; $pm->finish(0, { ret => $res, err => $@ }); } $pm->wait_all_children; } sub read_genome { # do something return { 'aa' => 'bb' }; } sub read_mapfile { # do something return { 'cc' => 'dd' }; } sub read_GTF { # do something die 'exception'; return { 'ee' => 'ff' }; # not reached } sub read_RM { # do something return { 'gg' => 'hh' }; } # use data generated in the subroutines use Data::Dumper; for my $id (0 .. $#task) { say "## ", $name[$id]; if (length $err[$id]) { say "ERROR: ", $err[$id]; next; } say Dumper($ret[$id]); }

Output:

## read_genome $VAR1 = { 'aa' => 'bb' }; ## read_mapfile $VAR1 = { 'cc' => 'dd' }; ## read_GTF ERROR: exception at j1.pl line 54. ## read_RM $VAR1 = { 'gg' => 'hh' };

Regards, Mario


In reply to Re: Return all the data from child to parent with Parallel::Forkmanager by marioroy
in thread Return all the data from child to parent with Parallel::Forkmanager by Microcebus

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.