NAME

Proc::Forkmap - map with forking

SYNOPSIS

EXAMPLE:

  use Proc::Forkmap qw(forkmap);

  $Proc::Forkmap::MAX_PROC = 4;

  sub foo {
    my $n = shift;
    sleep($n);
    print "slept for $n seconds\n";
  }

  my @x = (1, 2, 3);

  forkmap { foo($_) } @x;

  # Or OO interface, if you like that sort of thing

  use Proc::Forkmap;

  sub foo {
    my $x = shift;
    my $t = sprintf("%1.0f", $x + 1);
    sleep $t;
    print "slept $t seconds\n";
  }

  my @x = (rand(), rand(), rand());
  my $p = Proc::Forkmap->new;
  $p->fmap(\&foo, @x);

DESCRIPTION

This module provides mapping with built-in forking.

FUNCTIONS

forkmap

  forkmap { foo($_) } @x;

VARIABLES

These our variables control only the functional interface.

MAX_PROC

Max parallelism.

  $Proc::Forkmap::MAX_PROC = 4;

TIMEOUT

Max time in seconds any single child process can run.

  $Proc::Forkmap::TIMEOUT = 2;

METHODS

new

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

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

ipc

Set IPC on/off state. IPC is off by default.

non_blocking

Defaults to 1, and falsy to block.

max_kids

  $p->max_kids(4);

max_kids setter/getter.

fmap

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

This method takes a coderef and an array. If IPC is blocking, then it will return a result set. Otherwise, it will continue, waiting for child processes to complete their tasks.

ipc

  $p->ipc(1);

Turn on/off inter-process communication.

non_blocking

  $p->non_blocking(1);

If IPC is on, then set IO::Handle blocking state. This might be useful for conditional parallelism.

timeout

Timeout in seconds.

  $p->timeout(2);

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 2019 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.