Hi tybalt89,

That looks like a fun demonstration and gave it a spin. I commented out sleep and changed the input to 'job0001' .. 'job9999'. Then tried MCE implementations, mainly to compare overhead. MCE::Loop and MCE::Flow are wantarray-aware and construct a gather option with corresponding gather function automatically.

chunk_size => 1

#!/usr/bin/perl # https://perlmonks.org/?node_id=1217948 use strict; use warnings; use Data::Dump 'dd'; use Time::HiRes qw( time sleep ); use MCE::Loop; my $maxforks = 20; my @ids = 'job0001' .. 'job9999'; # it is cool that this works my $start = time; MCE::Loop->init( max_workers => $maxforks, chunk_size => 1 ); my @answers = mce_loop { my $id = $_; # sleep rand; my $ret = { id => $id, pid => $$, time => time - $start }; MCE->gather($ret); } \@ids; MCE::Loop->finish; dd \@answers;

chunk_size => 'auto'

#!/usr/bin/perl # https://perlmonks.org/?node_id=1217948 use strict; use warnings; use Data::Dump 'dd'; use Time::HiRes qw( time sleep ); use MCE::Loop; my $maxforks = 20; my @ids = 'job0001' .. 'job9999'; # it is cool that this works my $start = time; MCE::Loop->init( max_workers => $maxforks, chunk_size => 'auto' ); my @answers = mce_loop { my @ret; for my $id ( @{ $_ } ) { # sleep rand; push @ret, { id => $id, pid => $$, time => time - $start }; } MCE->gather(@ret); } \@ids; MCE::Loop->finish; dd \@answers;

Disclaimer. This is comparing apples to oranges because tybalt89's demonstration involves spawning a worker per each element. For the MCE demonstrations, workers request the manager process the next input element(s).

On my laptop (macOS and 9,999 iterations), tybalt89's example takes 16 seconds versus 0.6 and 0.5 seconds respectively for the MCE demonstrations.

Kind regards, Mario


In reply to Re^2: Parallel::ForkManager and multiple datasets by marioroy
in thread Parallel::ForkManager and multiple datasets by Speed_Freak

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.