The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MCE::Loop - Parallel loop model for building creative loops

VERSION

This document describes MCE::Loop version 1.499_004

SYNOPSIS CHUNK_SIZE == 1

All models in MCE default to 'auto' for chunk_size. MCE::Loop is not MCE::Map. The code block is configured as user_func for MCE. Therefore, a chunk_size > 1 means having to loop through each item in the chunk, similar to using MCE and creating a function for the user_func option and chunking through the data.

Think of MCE::Loop as a quick way to spun up a MCE instance with user_func set to your code block. You have the option of disabling/enabling chunking simply by setting chunk_size as shown below.

   ## Exports mce_loop, mce_loop_f, and mce_loop_s
   use MCE::Loop;

   MCE::Loop::init {
      chunk_size => 1
   };

   ## Array or array_ref
   mce_loop { do_work($_) } 1..10000;
   mce_loop { do_work($_) } [ 1..10000 ];

   ## File_path, glob_ref, or scalar_ref
   mce_loop_f { chomp; do_work($_) } "/path/to/file";
   mce_loop_f { chomp; do_work($_) } $file_handle;
   mce_loop_f { chomp; do_work($_) } \$scalar;

   ## Sequence of numbers (begin, end [, step, format])
   mce_loop_s { do_work($_) } 1, 10000, 5;
   mce_loop_s { do_work($_) } [ 1, 10000, 5 ];

   mce_loop_s { do_work($_) } {
      begin => 1, end => 10000, step => 5, format => undef
   };

SYNOPSIS CHUNK_SIZE > 1

Use this synopsis when chunk_size is set to 'auto' or greater than 1.

   use MCE::Loop;

   MCE::Loop::init {          ## Chunk_size defaults to 'auto' when
      chunk_size => 'auto'    ## not specified. Therefore, the init
   };                         ## function is not needed.

   ## Syntax is shown for mce_loop for demonstration purposes.
   ## Chunking also applies to mce_loop_f and mce_loop_s.
   mce_loop { do_work($_) for (@{ $_ }) } 1..10000;

   ## Same as above. This resembles "core" MCE code.
   mce_loop {
      my ($mce, $chunk_ref, $chunk_id) = @_;
      for (@{ $chunk_ref }) {
         do_work($_);
      }
   } 1..10000;

DESCRIPTION

TODO

OVERRIDING DEFAULTS

The following list 5 options which may be overridden when loading the module.

   use Sereal qw(encode_sereal decode_sereal);

   use MCE::Loop
         max_workers => 4,                    ## Default 'auto'
         chunk_size  => 100,                  ## Default 'auto'
         tmp_dir     => "/path/to/app/tmp",   ## $MCE::Signal::tmp_dir
         freeze      => \&encode_sereal,      ## \&Storable::freeze
         thaw        => \&decode_sereal       ## \&Storable::thaw
   ;

There is a simplier way to enable Sereal with MCE 1.5. The following will attempt to use Sereal if available, otherwise will default back to using Storable for serialization.

   use MCE::Loop Sereal => 1;

   MCE::Loop::init {
      chunk_size => 1
   };

   ## Serialization is through Sereal if available.
   my %answer = mce_loop { MCE->gather( $_, sqrt $_ ) } 1..10000;

CUSTOMIZING MCE

init

The init function takes a hash of MCE options.

   use MCE::Loop;

   MCE::Loop::init {
      chunk_size => 1, max_workers => 4,

      user_begin => sub {
         print "## ", MCE->wid, " started\n";
      },

      user_end => sub {
         print "## ", MCE->wid, " completed\n";
      }
   };

   my %a = mce_loop { MCE->gather($_, $_ * $_) } 1..100;

   print "\n", "@a{1..100}", "\n";

   -- output

   ## 3 started
   ## 1 started
   ## 2 started
   ## 4 started
   ## 1 completed
   ## 2 completed
   ## 3 completed
   ## 4 completed

   1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361
   400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156
   1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209
   2304 2401 2500 2601 2704 2809 2916 3025 3136 3249 3364 3481 3600
   3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041 5184 5329
   5476 5625 5776 5929 6084 6241 6400 6561 6724 6889 7056 7225 7396
   7569 7744 7921 8100 8281 8464 8649 8836 9025 9216 9409 9604 9801
   10000

API USAGE

TODO

MANUAL SHUTDOWN

finish

MCE workers remain persistent as much as possible after running. Shutdown occurs when the script exits. One can manually shutdown MCE by simply calling finish after running. This resets the MCE instance.

   use MCE::Loop;

   MCE::Loop::init {
      chunk_size => 20, max_workers => 'auto'
   };

   mce_loop { ... } 1..100;

   MCE::Loop::finish;

INDEX

MCE

AUTHOR

Mario E. Roy, <marioeroy AT gmail DOT com>

LICENSE

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.