use Parallel::ForkManager

A very powerful and easy to use Perl multitasking module.

Used in every single code I now write, as it’s annoying to continuously  update code just to make it scale-able. In my scripts I usually like to “fork” things off especially say when manipulating data.

With this module it’s as easy as a foreach loop:

use Parallel::ForkManager;

use PiroLabs::Utils::DataCruncher;

my $max_threads = 10;

my @tasks = qw (x / + -);

$pm = new Parallel::ForkManager($max_threads);

foreach (@tasks){

$pm = new Parallel::ForkManager($max_threads);

my $pid = $pm->start and next;

my $dc = new DataCruncher([0 … 2000000], $_);

$dc->Execute();

$pm->finish;

}

$pm->wait_all_children;

In the snippet above I want to perform multiplication, division, addition and subtraction all at the same time on some defined data set.

Next post I will show how to pass data from the child processes back to the parent.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s