MCE - map


The MCE package provides a map-like function that automatically runs the different tasks in separate processes then collects the results in the correct order.

By default it creates 4 child processes, but you can control that and a few other things by calling the init method.


examples/forks/use_mce_map.pl
use strict;
use warnings;

use MCE::Map;

main();

sub main {
    print "main PID: $$\n";
    my @results = mce_map { work($_) } 1..10;
    print "Results: @results\n";
}

sub work {
    my ($param) = @_;
    print "Param $param PID: $$\n";
    return $param * $param;
}

main PID: 150164
Param 1 PID: 150168
Param 2 PID: 150168
Param 7 PID: 150167
Param 8 PID: 150167
Param 3 PID: 150165
Param 5 PID: 150166
Param 4 PID: 150165
Param 6 PID: 150166
Param 9 PID: 150168
Param 10 PID: 150168
Results: 1 4 9 16 25 36 49 64 81 100