Hi again,

The following provides a MCE::Loop demonstration. This time, am using a hash for storing the returned data. Workers pass data using socket handles inside MCE. Thus, no temp files are made for sending data to the parent process.

use strict; use warnings; use feature 'say'; use MCE::Loop; 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; if ($threads == 1) { for my $id (0 .. $#task) { $ret{"$id"} = eval { $task[$id]->() }; $ret{"$id:err"} = $@; } } else { MCE::Loop->init( max_workers => $threads, posix_exit => 1, chunk_size => 1 ); %ret = mce_loop { my $id = $_; my $res = eval { $task[$id]->() }; MCE->gather( $id => $res, "$id:err" => $@ ); } [ 0 .. $#task ]; MCE::Loop->finish; } 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 $ret{"$id:err"}) { say "ERROR: ", $ret{"$id:err"}; next; } say Dumper($ret{$id}); }

Output:

## read_genome $VAR1 = { 'aa' => 'bb' }; ## read_mapfile $VAR1 = { 'cc' => 'dd' }; ## read_GTF ERROR: exception at j2.pl line 45, <__ANONIO__> line 2. ## read_RM $VAR1 = { 'gg' => 'hh' };

Regards, Mario


In reply to Re^2: 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.