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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |