Here's a way that avoids (for no particular good reason ) writing disk files by using perl's forked open. It also avoids eval (for maybe slightly better reasons ).

#!/usr/bin/perl # http://perlmonks.org/?node_id=1197603 use strict; use warnings; use Data::Dumper; use Storable qw( freeze thaw ); use IO::Select; my $sel = IO::Select->new; my %returndata; # this way you don't need to do an eval my @subs = (\&read_genome, \&read_mapfile, \&read_GTF, \&read_RM); for my $sub (@subs) # start all forks { if(open my $fh, '-|') { $sel->add($fh); $returndata{$fh} = ''; } else # child { print freeze $sub->(); exit; } } while( $sel->count ) # get return data { for my $fh ( $sel->can_read ) { if( 0 >= sysread $fh, $returndata{$fh}, 16 * 1024, length $returndata{$fh} ) { my $answer = thaw delete $returndata{$fh}; $sel->remove($fh); print Dumper $answer; # or whatever you want to do with it } } } sub read_genome { # do something select undef, undef, undef, .1 + rand 1; # simulate processing tim +e return { from => 'read_genome', results => { 1..4} }; } sub read_mapfile { # do something select undef, undef, undef, .1 + rand 1; # simulate processing tim +e return { from => 'read_mapfile', results => { 5..8} }; } sub read_GTF { # do something select undef, undef, undef, .1 + rand 1; # simulate processing tim +e return { from => 'read_GTF', results => { 1..10} }; } sub read_RM { # do something select undef, undef, undef, .1 + rand 1; # simulate processing tim +e return { from => 'read_RM', results => { 2..5} }; }

Prints (in a different order each time it's run)

$VAR1 = { 'results' => { '2' => 3, '4' => 5 }, 'from' => 'read_RM' }; $VAR1 = { 'results' => { '5' => 6, '1' => 2, '9' => 10, '7' => 8, '3' => 4 }, 'from' => 'read_GTF' }; $VAR1 = { 'from' => 'read_genome', 'results' => { '3' => 4, '1' => 2 } }; $VAR1 = { 'from' => 'read_mapfile', 'results' => { '7' => 8, '5' => 6 } };

In reply to Re: Return all the data from child to parent with Parallel::Forkmanager by tybalt89
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.