MCE::Flow(3)

🌈 MCE::Flow πŸ’–

okay bestie, so this is the manual page for MCE::Flow from the User Contributed Perl Documentation section (that's your (3) up in the corner, iykyk). Sit down, grab a lil snack, we're doing this. πŸ“


✨ NAME ✨

MCE::Flow β€” Parallel flow model for building creative applications 🌸


πŸŽ€ VERSION πŸŽ€

This document describes MCE::Flow version 1.902. periodt.


πŸ’… DESCRIPTION πŸ’…

MCE::Flow is literally the moment for writing custom apps that max out every single core you've got. This module was created to help you harness user_tasks within MCE. she's a team player, fr fr. 🫢

It is trivial to parallelize with mce_stream, shown below:

## Native map function
my @a = map { $_ * 4 } map { $_ * 3 } map { $_ * 2 } 1..10000;

## Same as with MCE::Stream (processing from right to left)
@a = mce_stream
     sub { $_ * 4 }, sub { $_ * 3 }, sub { $_ * 2 }, 1..10000;

## Pass an array reference to have writes occur simultaneously
mce_stream \@a,
     sub { $_ * 4 }, sub { $_ * 3 }, sub { $_ * 2 }, 1..10000;

But bestie, let's have MCE::Flow compute the same thing in parallel. MCE::Queue is gonna be our data flow bestie among the sub-tasks. πŸ¦‹

use MCE::Flow;
use MCE::Queue;

This calls for preserving output order. (yes queen, order matters πŸ’…)

sub preserve_order {
   my %tmp; my $order_id = 1; my $gather_ref = $_[0];
   @{ $gather_ref } = ();  ## clear the array (optional)

   return sub {
      my ($data_ref, $chunk_id) = @_;
      $tmp{$chunk_id} = $data_ref;

      while (1) {
         last unless exists $tmp{$order_id};
         push @{ $gather_ref }, @{ delete $tmp{$order_id++} };
      }

      return;
   };
}

Two queues are needed for data flow between the 3 sub-tasks. Notice task_end and how the value from $task_name is used for determining which task has ended. πŸ‘‘

my $b = MCE::Queue->new;
my $c = MCE::Queue->new;

sub task_end {
   my ($mce, $task_id, $task_name) = @_;

   if (defined $mce->{user_tasks}->[$task_id + 1]) {
      my $n_workers = $mce->{user_tasks}->[$task_id + 1]->{max_workers};

      if ($task_name eq 'a') {
         $b->enqueue((undef) x $n_workers);
      }
      elsif ($task_name eq 'b') {
         $c->enqueue((undef) x $n_workers);
      }
   }

   return;
}

Next up are the 3 sub-tasks. The first one reads input and begins the flow. The 2nd task dequeues, performs the calculation, and enqueues into the next. Finally, the last task calls the gather method. ✨

Although serialization is done for you automatically, it is done here to save from double serialization. This is the fastest approach for passing data between sub-tasks. Thus, the least overhead. no cap.

sub task_a {
   my @ans; my ($mce, $chunk_ref, $chunk_id) = @_;

   push @ans, map { $_ * 2 } @{ $chunk_ref };
   $b->enqueue(MCE->freeze([ \@ans, $chunk_id ]));

   return;
}

sub task_b {
   my ($mce) = @_;

   while (1) {
      my @ans; my $chunk = $b->dequeue;
      last unless defined $chunk;

      $chunk = MCE->thaw($chunk);
      push @ans, map { $_ * 3 } @{ $chunk->[0] };
      $c->enqueue(MCE->freeze([ \@ans, $chunk->[1] ]));
   }

   return;
}

sub task_c {
   my ($mce) = @_;

   while (1) {
      my @ans; my $chunk = $c->dequeue;
      last unless defined $chunk;

      $chunk = MCE->thaw($chunk);
      push @ans, map { $_ * 4 } @{ $chunk->[0] };
      MCE->gather(\@ans, $chunk->[1]);
   }

   return;
}

In summary (the way that I need you to remember this 😭), MCE::Flow builds out a MCE instance behind the scene and starts running. The task_name (shown), max_workers, and use_threads options can take an anonymous array for specifying the values uniquely per each sub-task. iconic.

my @a;

mce_flow {
   task_name => [ 'a', 'b', 'c' ], task_end => \&task_end,
   gather => preserve_order(\@a)

}, \&task_a, \&task_b, \&task_c, 1..10000;

print "@a\n";

If speed is not a concern and you're wanting to rid of all the MCE->freeze and MCE->thaw statements, simply enqueue and dequeue 2 items at a time. Or better yet, see MCE::Step introduced in MCE 1.506. 🌟

First, task_end must be updated. The number of undef(s) must match the number of workers times the dequeue count. Otherwise, the script will stall. 🚨

sub task_end {
   ...
      if ($task_name eq 'a') {
       # $b->enqueue((undef) x $n_workers);
         $b->enqueue((undef) x ($n_workers * 2));
      }
      elsif ($task_name eq 'b') {
       # $c->enqueue((undef) x $n_workers);
         $c->enqueue((undef) x ($n_workers * 2));
      }
   ...
}

Next, the 3 sub-tasks enqueuing and dequeuing 2 elements at a time. 🧚

sub task_a {
   my @ans; my ($mce, $chunk_ref, $chunk_id) = @_;

   push @ans, map { $_ * 2 } @{ $chunk_ref };
   $b->enqueue(\@ans, $chunk_id);

   return;
}

sub task_b {
   my ($mce) = @_;

   while (1) {
      my @ans; my ($chunk_ref, $chunk_id) = $b->dequeue(2);
      last unless defined $chunk_ref;

      push @ans, map { $_ * 3 } @{ $chunk_ref };
      $c->enqueue(\@ans, $chunk_id);
   }

   return;
}

sub task_c {
   my ($mce) = @_;

   while (1) {
      my @ans; my ($chunk_ref, $chunk_id) = $c->dequeue(2);
      last unless defined $chunk_ref;

      push @ans, map { $_ * 4 } @{ $chunk_ref };
      MCE->gather(\@ans, $chunk_id);
   }

   return;
}

Finally, run as usual. slay. πŸ’…

my @a;

mce_flow {
   task_name => [ 'a', 'b', 'c' ], task_end => \&task_end,
   gather => preserve_order(\@a)

}, \&task_a, \&task_b, \&task_c, 1..10000;

print "@a\n";

πŸ¦‹ SYNOPSIS when CHUNK_SIZE EQUALS 1 πŸ¦‹

Although MCE::Loop may be preferred for running using a single code block, the text below also applies to this module, particularly for the first block. πŸ’œ

All models in MCE default to 'auto' for chunk_size. The arguments for the block are the same as writing a user_func block using the Core API.

Beginning with MCE 1.5, the next input item is placed into the input scalar variable $_ when chunk_size equals 1. Otherwise, $_ points to $chunk_ref containing many items. Basically, line 2 below may be omitted from your code when using $_. One can call MCE->chunk_id to obtain the current chunk id. πŸ”₯

line 1:  user_func => sub {
line 2:     my ($mce, $chunk_ref, $chunk_id) = @_;
line 3:
line 4:     $_ points to $chunk_ref->[0]
line 5:        in MCE 1.5 when chunk_size == 1
line 6:
line 7:     $_ points to $chunk_ref
line 8:        in MCE 1.5 when chunk_size  > 1
line 9:  }

Follow this synopsis when chunk_size equals one. Looping is not required from inside the first block. Hence, the block is called once per each item. ✨

## Exports mce_flow, mce_flow_f, and mce_flow_s
use MCE::Flow;

MCE::Flow->init(
   chunk_size => 1
);

## Array or array_ref
mce_flow sub { do_work($_) }, 1..10000;
mce_flow sub { do_work($_) }, \@list;

## Important; pass an array_ref for deeply input data
mce_flow sub { do_work($_) }, [ [ 0, 1 ], [ 0, 2 ], ... ];
mce_flow sub { do_work($_) }, \@deeply_list;

## File path, glob ref, IO::All::{ File, Pipe, STDIO } obj, or scalar ref
## Workers read directly and not involve the manager process
mce_flow_f sub { chomp; do_work($_) }, "/path/to/file"; # efficient

## Involves the manager process, therefore slower
mce_flow_f sub { chomp; do_work($_) }, $file_handle;
mce_flow_f sub { chomp; do_work($_) }, $io;
mce_flow_f sub { chomp; do_work($_) }, \$scalar;

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

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

🌸 SYNOPSIS when CHUNK_SIZE is GREATER THAN 1 🌸

Follow this synopsis when chunk_size equals 'auto' or greater than 1. This means having to loop through the chunk from inside the first block. 😌

use MCE::Flow;

MCE::Flow->init(           ## Chunk_size defaults to 'auto' when
   chunk_size => 'auto'    ## not specified. Therefore, the init
);                         ## function may be omitted.

## Syntax is shown for mce_flow for demonstration purposes.
## Looping inside the block is the same for mce_flow_f and
## mce_flow_s.

## Array or array_ref
mce_flow sub { do_work($_) for (@{ $_ }) }, 1..10000;
mce_flow sub { do_work($_) for (@{ $_ }) }, \@list;

## Important; pass an array_ref for deeply input data
mce_flow sub { do_work($_) for (@{ $_ }) }, [ [ 0, 1 ], [ 0, 2 ], ... ];
mce_flow sub { do_work($_) for (@{ $_ }) }, \@deeply_list;

## Resembles code using the core MCE API
mce_flow sub {
   my ($mce, $chunk_ref, $chunk_id) = @_;

   for (@{ $chunk_ref }) {
      do_work($_);
   }

}, 1..10000;

Chunking reduces the number of IPC calls behind the scene. Think in terms of chunks whenever processing a large amount of data. For relatively small data, choosing 1 for chunk_size is fine. ⭐


πŸ‘‘ OVERRIDING DEFAULTS πŸ‘‘

The following list options which may be overridden when loading the module. πŸ’…

use Sereal qw( encode_sereal decode_sereal );
use CBOR::XS qw( encode_cbor decode_cbor );
use JSON::XS qw( encode_json decode_json );

use MCE::Flow
    max_workers => 8,                # Default 'auto'
    chunk_size => 500,               # Default 'auto'
    tmp_dir => "/path/to/app/tmp",   # $MCE::Signal::tmp_dir
    freeze => \&encode_sereal,       # \&Storable::freeze
    thaw => \&decode_sereal,         # \&Storable::thaw
    init_relay => 0,                 # Default undef; MCE 1.882+
    use_threads => 0,                # Default undef; MCE 1.882+
;

From MCE 1.8 onwards, Sereal 3.015+ is loaded automatically if available. Specify "Sereal => 0" to use Storable instead. πŸ™ˆ

use MCE::Flow Sereal => 0;

πŸŽ€ CUSTOMIZING MCE πŸŽ€

πŸ’– MCE::Flow->init ( options ) πŸ’–

πŸ’– MCE::Flow::init { options } πŸ’–

okay so the init function takes a hash of MCE options, that's it, that's the whole vibe. and here's the tea: unlike with MCE::Stream, you can totally specify both the gather and bounds_only options here (the example below just doesn't show them, but bestie, they're allowed πŸ’…).

and in scalar context (this API has been around since 1.897), it'll call MCE::Flow->finish automatically when you leave the scope or the program ends. self-cleaning queen behavior, we love to see it ✨

use MCE::Flow;

my $guard = MCE::Flow->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_flow sub { MCE->gather($_, $_ * $_) }, 1..100;

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

-- Output

## 3 started
## 2 started
## 4 started
## 1 started
## 2 completed
## 4 completed
## 3 completed
## 1 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

just like with MCE::Flow->init up there πŸ‘†, you can hand over your MCE options as an anonymous hash for the first argument. and notice how task_name, max_workers, and use_threads can each take an anonymous array so you can set them uniquely for every single code block. one array, many tasks, chef's kiss 😌

and ok here's a big difference: unlike MCE::Stream which processes right-to-left, MCE::Flow starts with the first code block, so it processes left-to-right. first come, first served, periodt πŸ’…

use threads;
use MCE::Flow;

my @a = mce_flow {
   task_name   => [ 'a', 'b', 'c' ],
   max_workers => [  3,   4,   2, ],
   use_threads => [  1,   0,   0, ],

   user_end => sub {
      my ($mce, $task_id, $task_name) = @_;
      MCE->print("$task_id - $task_name completed\n");
   },

   task_end => sub {
      my ($mce, $task_id, $task_name) = @_;
      MCE->print("$task_id - $task_name ended\n");
   }
},
sub { sleep 1; },   ## 3 workers, named a
sub { sleep 2; },   ## 4 workers, named b
sub { sleep 3; };   ## 2 workers, named c

-- Output

0 - a completed
0 - a completed
0 - a completed
0 - a ended
1 - b completed
1 - b completed
1 - b completed
1 - b completed
1 - b ended
2 - c completed
2 - c completed
2 - c ended

✨ API DOCUMENTATION ✨

ngl, input data is technically optional for MCE::Flow, but for everything below we're just gonna pretend chunk_size equals 1 so we can show you literally every possible way to feed it data. all the options, all the flavors, no crumbs left behind πŸ“

πŸ‘‘ MCE::Flow->run ( sub { code }, list ) πŸ‘‘

πŸ‘‘ mce_flow sub { code }, list πŸ‘‘

so your input data can be a list, an array ref, or a hash ref. your choice bestie 🫢

but here's the thing that trips people up: unlike MCE::Loop, Map, and Grep which take a block written as { ... }, Flow wants a sub { ... } or a code reference. and the other difference is that you need the comma after the block. yes, the comma. it's not optional. it's the main character. don't forget it πŸ™ˆ

# $_ contains the item when chunk_size => 1

mce_flow sub { do_work($_) }, 1..1000;
mce_flow sub { do_work($_) }, \@list;

# Important; pass an array_ref for deeply input data

mce_flow sub { do_work($_) }, [ [ 0, 1 ], [ 0, 2 ], ... ];
mce_flow sub { do_work($_) }, \@deeply_list;

# Chunking; any chunk_size => 1 or greater

my %res = mce_flow sub {
   my ($mce, $chunk_ref, $chunk_id) = @_;
   my %ret;
   for my $item (@{ $chunk_ref }) {
      $ret{$item} = $item * 2;
   }
   MCE->gather(%ret);
},
\@list;

# Input hash; current API available since 1.828

my %res = mce_flow sub {
   my ($mce, $chunk_ref, $chunk_id) = @_;
   my %ret;
   for my $key (keys %{ $chunk_ref }) {
      $ret{$key} = $chunk_ref->{$key} * 2;
   }
   MCE->gather(%ret);
},
\%hash;

# Unlike MCE::Loop, MCE::Flow doesn't need input to run

mce_flow { max_workers => 4 }, sub {
   MCE->say( MCE->wid );
};

# ... and can run multiple tasks

mce_flow {
   max_workers => [  1,   3  ],
   task_name   => [ 'p', 'c' ]
},
sub {
   # 1 producer
   MCE->say( "producer: ", MCE->wid );
},
sub {
   # 3 consumers
   MCE->say( "consumer: ", MCE->wid );
};

# Here, options are specified via init

MCE::Flow->init(
   max_workers => [  1,   3  ],
   task_name   => [ 'p', 'c' ]
);

mce_flow \&producer, \&consumers;

πŸ¦‹ MCE::Flow->run_file ( sub { code }, file ) πŸ¦‹

πŸ¦‹ mce_flow_f sub { code }, file πŸ¦‹

and the fastest of all these is the /path/to/file version. the workers just pass the next offset position amongst themselves and the manager process does literally nothing β€” zero interaction, zero drama, just vibes πŸ”₯

IO::All { File, Pipe, STDIO } is supported since MCE 1.845. so yes, you can be fancy with it πŸ’…

# $_ contains the line when chunk_size => 1

mce_flow_f sub { $_ }, "/path/to/file";  # faster
mce_flow_f sub { $_ }, $file_handle;
mce_flow_f sub { $_ }, $io;              # IO::All
mce_flow_f sub { $_ }, \$scalar;

# chunking, any chunk_size => 1 or greater

my %res = mce_flow_f sub {
   my ($mce, $chunk_ref, $chunk_id) = @_;
   my $buf = '';
   for my $line (@{ $chunk_ref }) {
      $buf .= $line;
   }
   MCE->gather($chunk_id, $buf);
},
"/path/to/file";

🌈 MCE::Flow->run_seq ( sub { code }, $beg, $end [, $step, $fmt ] ) 🌈

🌈 mce_flow_s sub { code }, $beg, $end [, $step, $fmt ] 🌈

so a sequence can be a list, an array reference, or a hash reference. but you gotta give it both a begin and an end value or it won't run β€” that's not negotiable bestie. step and format are optional though, live your life. and the format gets passed to sprintf (the % may be omitted below, if you're feeling casual).

my ($beg, $end, $step, $fmt) = (10, 20, 0.1, "%4.1f");

# $_ contains the sequence number when chunk_size => 1

mce_flow_s sub { $_ }, $beg, $end, $step, $fmt;
mce_flow_s sub { $_ }, [ $beg, $end, $step, $fmt ];

mce_flow_s sub { $_ }, {
   begin => $beg, end => $end,
   step => $step, format => $fmt
};

# chunking, any chunk_size => 1 or greater

my %res = mce_flow_s sub {
   my ($mce, $chunk_ref, $chunk_id) = @_;
   my $buf = '';
   for my $seq (@{ $chunk_ref }) {
      $buf .= "$seq\n";
   }
   MCE->gather($chunk_id, $buf);
},
[ $beg, $end ];

okay so the sequence engine has a special little trick ✨ β€” it can compute just the 'begin' and 'end' items for the chunk and skip everything in between (hence "boundaries only"). this option only applies to sequences and does nothing at all when chunk_size equals 1.

and the numbers here are genuinely unhinged: it takes 0.006s below. without the bounds_only option it becomes 0.827s, because it has to compute every single item in between and that creates a very large array. so basically, set bounds_only => 1 whenever boundaries is all you actually need for looping inside the block β€” like Monte Carlo simulations, iykyk 🎲

(time was measured using 1 worker to really emphasize the difference. one worker. just to show off how much faster it is πŸ’…)

use MCE::Flow;

MCE::Flow->init(
   max_workers => 1, chunk_size => 1_250_000,
   bounds_only => 1
);

# Typically, the input scalar $_ contains the sequence number
# when chunk_size => 1, unless the bounds_only option is set
# which is the case here. Thus, $_ points to $chunk_ref.

mce_flow_s sub {
   my ($mce, $chunk_ref, $chunk_id) = @_;

   # $chunk_ref contains 2 items, not 1_250_000
   # my ( $begin, $end ) = ( $_->[0], $_->[1] );

   my $begin = $chunk_ref->[0];
   my $end   = $chunk_ref->[1];

   # for my $seq ( $begin .. $end ) {
   #    ...
   # }

   MCE->printf("%7d .. %8d\n", $begin, $end);
},
[ 1, 10_000_000 ];

-- Output

      1 ..  1250000
1250001 ..  2500000
2500001 ..  3750000
3750001 ..  5000000
5000001 ..  6250000
6250001 ..  7500000
7500001 ..  8750000
8750001 .. 10000000

πŸ’œ MCE::Flow->run ( { input_data => iterator }, sub { code } ) πŸ’œ

πŸ’œ mce_flow { input_data => iterator }, sub { code } πŸ’œ

you can hand over an iterator reference for input_data, sure. but heads up bestie β€” the only other way to do it is to specify input_data through MCE::Flow->init. why? because that stops MCE::Flow from configuring the iterator reference as another user task, which straight up won't work. so don't do that πŸ™ˆ

iterators are described over in the section "SYNTAX for INPUT_DATA" at MCE::Core.

MCE::Flow->init(
   input_data => iterator
);

mce_flow sub { $_ };

πŸ’– GATHERING DATA πŸ’–

Okay bestie, so GATHERING DATA is where it gets spicy. With MCE::Map, gather and output order are literally done for you automatically. But here, the gather method is how you send results back to the manager process. No cap. 🫢

use MCE::Flow chunk_size => 1;

## Output order is not guaranteed.
my @a1 = mce_flow sub { MCE->gather($_ * 2) }, 1..100;
print "@a1\n\n";

## Outputs to a hash instead (key, value).
my %h1 = mce_flow sub { MCE->gather($_, $_ * 2) }, 1..100;
print "@h1{1..100}\n\n";

## This does the same thing due to chunk_id starting at one.
my %h2 = mce_flow sub { MCE->gather(MCE->chunk_id, $_ * 2) }, 1..100;
print "@h2{1..100}\n\n";

The gather method can be called multiple times inside the block β€” unlike return, which would leave the block. So think of gather as yielding results immediately to the manager process without actually leaving the block. It's giving "I'm still here but also here's a snack" energy. πŸ’…

use MCE::Flow chunk_size => 1, max_workers => 3;

my @hosts = qw(
   hosta hostb hostc hostd hoste
);

my %h3 = mce_flow sub {
   my ($output, $error, $status); my $host = $_;

   ## Do something with $host;
   $output = "Worker ". MCE->wid .": Hello from $host";

   if (MCE->chunk_id % 3 == 0) {
      ## Simulating an error condition
      local $? = 1; $status = $?;
      $error = "Error from $host"
   }
   else {
      $status = 0;
   }

   ## Ensure unique keys (key, value) when gathering to
   ## a hash.
   MCE->gather("$host.out", $output);
   MCE->gather("$host.err", $error) if (defined $error);
   MCE->gather("$host.sta", $status);

}, @hosts;

foreach my $host (@hosts) {
   print $h3{"$host.out"}, "\n";
   print $h3{"$host.err"}, "\n" if (exists $h3{"$host.err"});
   print "Exit status: ", $h3{"$host.sta"}, "\n\n";
}

Here's the output, bestie:

-- Output

Worker 3: Hello from hosta
Exit status: 0

Worker 2: Hello from hostb
Exit status: 0

Worker 1: Hello from hostc
Error from hostc
Exit status: 1

Worker 3: Hello from hostd
Exit status: 0

Worker 2: Hello from hoste
Exit status: 0

The next one uses an anonymous array containing 3 elements when gathering data. Serialization is automatic behind the scene. No manual packing, bestie. ✨

my %h3 = mce_flow sub {
   ...

   MCE->gather($host, [$output, $error, $status]);

}, @hosts;

foreach my $host (@hosts) {
   print $h3{$host}->[0], "\n";
   print $h3{$host}->[1], "\n" if (defined $h3{$host}->[1]);
   print "Exit status: ", $h3{$host}->[2], "\n\n";
}

Although MCE::Map comes to mind, sometimes you want extra control when gathering data, like retaining output order. Enter this iconic little subroutine:

use MCE::Flow;

sub preserve_order {
   my %tmp; my $order_id = 1; my $gather_ref = $_[0];

   return sub {
      $tmp{ (shift) } = \@_;

      while (1) {
         last unless exists $tmp{$order_id};
         push @{ $gather_ref }, @{ delete $tmp{$order_id++} };
      }

      return;
   };
}

## Workers persist for the most part after running. Though, not always
## the case and depends on Perl. Pass a reference to a subroutine if
## workers must persist; e.g. mce_flow { ... }, \&foo, 1..100000.

MCE::Flow->init(
   chunk_size => 'auto', max_workers => 'auto'
);

for (1..2) {
   my @m2;

   mce_flow {
      gather => preserve_order(\@m2)
   },
   sub {
      my @a; my ($mce, $chunk_ref, $chunk_id) = @_;

      ## Compute the entire chunk data at once.
      push @a, map { $_ * 2 } @{ $chunk_ref };

      ## Afterwards, invoke the gather feature, which
      ## will direct the data to the callback function.
      MCE->gather(MCE->chunk_id, @a);

   }, 1..100000;

   print scalar @m2, "\n";
}

MCE::Flow->finish;

All 6 models support 'auto' for chunk_size unlike the Core API. Think of the models as the basis for providing JIT for MCE. They create the instance, tune max_workers, and tune chunk_size automatically regardless of the hardware. Slay. πŸ‘‘

The following does the same thing using the Core API. Workers persist after running.

use MCE;

sub preserve_order {
   ...
}

my $mce = MCE->new(
   max_workers => 'auto', chunk_size => 8000,

   user_func => sub {
      my @a; my ($mce, $chunk_ref, $chunk_id) = @_;

      ## Compute the entire chunk data at once.
      push @a, map { $_ * 2 } @{ $chunk_ref };

      ## Afterwards, invoke the gather feature, which
      ## will direct the data to the callback function.
      MCE->gather(MCE->chunk_id, @a);
   }
);

for (1..2) {
   my @m2;

   $mce->process({ gather => preserve_order(\@m2) }, [1..100000]);

   print scalar @m2, "\n";
}

$mce->shutdown;

πŸ›‘ MANUAL SHUTDOWN πŸ›‘

  • MCE::Flow->finish
  • MCE::Flow::finish

Workers remain persistent as much as possible after running. Shutdown occurs automatically when the script terminates. Call finish when workers are no longer needed. Like, when you're done, say bye. πŸ‘‹

use MCE::Flow;

MCE::Flow->init(
   chunk_size => 20, max_workers => 'auto'
);

mce_flow sub { ... }, 1..100;

MCE::Flow->finish;

πŸ“‡ INDEX πŸ“‡

  • MCE
  • MCE::Core

πŸ‘‘ AUTHOR πŸ‘‘

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


This page was brought to you by perl v5.42.0, dated 2025-09-10, from the MCE::Flow(3) manual. Stay iconic, bestie! πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from