NAME

Proc::Forkmap - map with forking and IPC

SYNOPSIS

EXAMPLES:

sub foo {
    my ($x,$n) = (shift,1);
    $n *= $_ for (@$x);
    say $n;
}

@x = ([1..99],[100..199],[200..299]);
my $p = Proc::Forkmap->new;
$p->fmap(\&foo,@x);

or,

package Foo;

sub new { return bless {}, shift};
sub bar { #do heavy calc stuff and max a CPU };

package main;

my $foo = Foo->new;
my @rs = Proc::Forkmap->new(max_kids => 4, ipc=> 1)->fmap(
    sub { $foo->bar(@_) }, @x,
);

or,

my @rs = $p->fmap(sub { $_[0] ** $_[0] }, @x);

or,

#get stuff from the intertubes

sub bar {
   my $t = shift;
   my $s = Stock::Price->new;
   ... get historical stock prices ...
   ... do some analysis ...
   baz($t,$sell_price);
}

#then save stuff to a data store

sub baz {
   my ($t,$price) = @_;
   my $conn = MongoDB::Connection->new;
   my $bayes = $conn->stock->bayes;
   $bayes->insert({symbol => $t, price => $price});
   $conn->disconnect;
}

my $p = Proc::Forkmap->new(max_kids => 4);
$p->fmap(\&bar,qw/rht goog ^ixic ^dji yhoo aapl/);

DESCRIPTION

This module supplies an easy to use map method that provides built-in forking and IPC.

METHODS

new

my $p = Proc::Forkmap->new(max_kids => 4, ipc => 1);
max_kids

Maximum number of kids allowed in the pool. The default is 2.

ipc

Set IPC on (blocking)/off state. IPC is off by default.

icp

$p->ipc(1)

Turn on/off inter-process communication.

max_kids

$p->max_kids(4);

max_kids setter/getter.

fmap

my @rs = $p->fmap(\&foo,@x);

This method takes a coderef and an array. If IPC is turned on, it will return, via IO::Pipe, a result set. Otherwise, it will continue on its merry way until either an error occurs that prevents a fork or all the subprocesses complete their tasks.

SEE ALSO

Proc::Fork, IO::Pipe

AUTHOR

Andrew Shapiro, <trski@cpan.org>

BUGS

Please report any bugs or feature requests to bug-proc-forkmap at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-Forkmap. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

LICENSE AND COPYRIGHT

Copyright 2012 Andrew Shapiro.

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.