Proc::Forkmap - map with forking and IPC
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/);
This module supplies an easy to use map method that provides built-in forking and IPC.
my $p = Proc::Forkmap->new(max_kids => 4, ipc => 1);
Maximum number of kids allowed in the pool. The default is 2.
Set IPC on (blocking)/off state. IPC is off by default.
$p->ipc(1)
Turn on/off inter-process communication.
$p->max_kids(4);
max_kids setter/getter.
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.
Proc::Fork, IO::Pipe
Andrew Shapiro, <trski@cpan.org>
<trski@cpan.org>
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.
bug-proc-forkmap at rt.cpan.org
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.
To install Proc::Forkmap, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Proc::Forkmap
CPAN shell
perl -MCPAN -e shell install Proc::Forkmap
For more information on module installation, please visit the detailed CPAN module installation guide.