| 1 | package Parallel::Tiny; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use POSIX qw(WNOHANG); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Sys::Prctl qw(prctl); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | our $VERSION = 1.00; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # defaults for prctl() | ||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use constant PR_SET_PDEATHSIG => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use constant SIGHUP => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # defaults for the new() method | ||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use constant DEFAULT_ERROR_TIMEOUT => 10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use constant DEFAULT_REAP_TIMEOUT => .1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use constant DEFAULT_SUBNAME => 'run'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use constant DEFAULT_WORKERS => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use constant DEFAULT_WORKER_TOTAL => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Parallel::Tiny | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Provides a simple, no frills fork manager. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Given an object that provides a C<run()> method, you can create a C<Parallel::Tiny> fork manager object that will execute that method several times. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $obj = My::Handler->new(); | ||||||||||||||||||||||||||||
| 33 | my $forker = Parallel::Tiny->new( | ||||||||||||||||||||||||||||
| 34 | handler => $obj, | ||||||||||||||||||||||||||||
| 35 | workers => 4, | ||||||||||||||||||||||||||||
| 36 | worker_total => 32, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | $forker->run(); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | In the above example we will execute the C<run()> method for a C<My::Handler> object 4 workers at a time, until 32 total workers have completed/died. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 METHODS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =over | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =item new() | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Returns a new C<Parallel::Tiny> fork manager. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Takes the following arguments as a hash or hashref: | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | handler => $handler, # provide an object with a run() method, this will be your worker (required) | ||||||||||||||||||||||||||||
| 54 | reap_timeout => $reap_timeout, # how long to wait in between reaping children (default ".1") | ||||||||||||||||||||||||||||
| 55 | subname => $subname, # a method name to execute for the $handler (default "run") | ||||||||||||||||||||||||||||
| 56 | workers => $workers, # the number of workers that can run simultaneously (default 1) | ||||||||||||||||||||||||||||
| 57 | worker_total => $worker_total, # the total number of times to run before stopping (default 1) | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | For instance, you could run 100 workers, 4 workers at a time: | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my $forker = Parallel::Tiny->new( | ||||||||||||||||||||||||||||
| 63 | handler => $obj, | ||||||||||||||||||||||||||||
| 64 | workers => 4, | ||||||||||||||||||||||||||||
| 65 | worker_total => 100, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | C<infinite> can be provided for the C<$worker_total> but you will need to manage stopping the fork manager elsewhere. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | If the parent is sent C<SIGTERM> it will wait to reap all currently executing children before finishing. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | If the parent is killed, children will receive C<SIGHUP>, which you will need to deal with in your C<$handler>. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 24 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=24.3
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 24 | my $class = shift; | |||||||||||||||||||||||||||
| 78 | 24 | ▶ | my $args = ref( $_[0] ) ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # set some defaults | ||||||||||||||||||||||||||||
| 81 | 24 | ▶ | $args->{reap_timeout} ||= DEFAULT_REAP_TIMEOUT; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"reap_timeout"} ||= "0.1"
MC/DC0%0/1
$args->{"reap_timeout"} ||= "0.1"
$args->{"reap_timeout"}
Truth table50%1/2
A
A= $args->{"reap_timeout"}
| |||||||||||||||||||||||||||||
| 82 | 24 | ▶ | $args->{subname} ||= DEFAULT_SUBNAME; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"subname"} ||= "run"
MC/DC0%0/1
$args->{"subname"} ||= "run"
$args->{"subname"}
Truth table50%1/2
A
A= $args->{"subname"}
| |||||||||||||||||||||||||||||
| 83 | 24 | ▶ | $args->{workers} ||= DEFAULT_WORKERS; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"workers"} ||= 1
MC/DC0%0/1
$args->{"workers"} ||= 1
$args->{"workers"}
Truth table50%1/2
A
A= $args->{"workers"}
| |||||||||||||||||||||||||||||
| 84 | 24 | ▶ | $args->{worker_total} ||= DEFAULT_WORKER_TOTAL; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"worker_total"} ||= 1
MC/DC0%0/1
$args->{"worker_total"} ||= 1
$args->{"worker_total"}
Truth table50%1/2
A
A= $args->{"worker_total"}
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # special configuration | ||||||||||||||||||||||||||||
| 87 | 24 | ▶ | undef $args->{worker_total} if $args->{worker_total} eq 'infinite'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # check args | ||||||||||||||||||||||||||||
| 90 | 24 | ▶ | die 'no handler provided' unless $args->{handler}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | die "handler doesn't implement $args->{subname}()" | ||||||||||||||||||||||||||||
| 92 | 24 | ▶ | unless $args->{handler}->can( $args->{subname} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | return bless( | ||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | _continue => 1, | ||||||||||||||||||||||||||||
| 97 | _handler => $args->{handler}, | ||||||||||||||||||||||||||||
| 98 | _jobs => {}, | ||||||||||||||||||||||||||||
| 99 | _reap_timeout => $args->{reap_timeout}, | ||||||||||||||||||||||||||||
| 100 | _subname => $args->{subname}, | ||||||||||||||||||||||||||||
| 101 | _workers => $args->{workers}, | ||||||||||||||||||||||||||||
| 102 | _worker_total => $args->{worker_total}, | ||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | 24 | $class | |||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item run() | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Start running a number of parallel workers equal to C<$workers>, until a number of workers equal to C<$worker_total> have been completed. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 21 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | local $SIG{TERM} = sub { $self->{_continue} = 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # setup the fork manager | ||||||||||||||||||||||||||||
| 120 | 21 | my $handler = $self->{_handler}; | |||||||||||||||||||||||||||
| 121 | 21 | my $subname = $self->{_subname}; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 21 | ▶ | while ( $self->_waitqueue() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # parent work | ||||||||||||||||||||||||||||
| 126 | 65 | my $pid = fork(); | |||||||||||||||||||||||||||
| 127 | 65 | ▶ | if ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | $self->{_worker_total}-- | ||||||||||||||||||||||||||||
| 129 | 55 | ▶ | if defined $self->{_worker_total} and $self->{_worker_total} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"_worker_total"} and $self->{"_worker_total"} > 0
MC/DC0%0/2
defined $self->{"_worker_total"} and $self->{"_worker_total"} > 0
defined $self->{"_worker_total"}
$self->{"_worker_total"} > 0
Truth table33%1/3
A and B
A= defined $self->{"_worker_total"}B= $self->{"_worker_total"} > 0
| |||||||||||||||||||||||||||||
| 130 | 55 | $self->{_jobs}{$pid} = 1; | |||||||||||||||||||||||||||
| 131 | 55 | next; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # child work | ||||||||||||||||||||||||||||
| 135 | 10 | prctl( PR_SET_PDEATHSIG, SIGHUP ); | |||||||||||||||||||||||||||
| 136 | 10 | $SIG{$_} = 'DEFAULT' for keys %SIG; | |||||||||||||||||||||||||||
| 137 | 10 | $0 = $0 . ' - worker'; | |||||||||||||||||||||||||||
| 138 | 10 | $handler->$subname(); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # child cleanup | ||||||||||||||||||||||||||||
| 141 | 10 | exit 0; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # wait for children | ||||||||||||||||||||||||||||
| 145 | 11 | ▶ | while ( wait() != -1 ) { } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 11 | return 1; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ##----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 151 | ## Private Methods | ||||||||||||||||||||||||||||
| 152 | ##----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # waits for another job slot to become available short circuits if we've received SIGTERM or reached worker total threshold | ||||||||||||||||||||||||||||
| 155 | 76 | ▶ | sub _waitqueue { | ||||||||||||||||||||||||||
|
Subroutine
_waitqueue: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 156 | 76 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # short circuit if we've already completed all the workers we've been configured to run | ||||||||||||||||||||||||||||
| 159 | 76 | ▶ | return 0 if defined $self->{_worker_total} and $self->{_worker_total} <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"_worker_total"} and $self->{"_worker_total"} <= 0
MC/DC50%1/2
defined $self->{"_worker_total"} and $self->{"_worker_total"} <= 0
defined $self->{"_worker_total"}
$self->{"_worker_total"} <= 0
Truth table66%2/3
A and B
A= defined $self->{"_worker_total"}B= $self->{"_worker_total"} <= 0
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # wait to reap at least one child | ||||||||||||||||||||||||||||
| 162 | 65 | ▶ | while ( keys %{ $self->{_jobs} } >= $self->{_workers} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 82985 | ▶ | return 0 unless $self->{_continue}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 82985 | $self->_reapchildren(); | |||||||||||||||||||||||||||
| 165 | 82985 | sleep $self->{_reap_timeout}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 65 | return 1; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # cleans up children that are no longer running | ||||||||||||||||||||||||||||
| 172 | 82985 | ▶ | sub _reapchildren { | ||||||||||||||||||||||||||
|
Subroutine
_reapchildren: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 173 | 82985 | my $self = shift; | |||||||||||||||||||||||||||
| 174 | 82985 | foreach my $pid ( keys %{ $self->{_jobs} } ) { | |||||||||||||||||||||||||||
| 175 | 165970 | my $waitpid = waitpid( $pid, WNOHANG ); | |||||||||||||||||||||||||||
| 176 | 165970 | ▶ | delete $self->{_jobs}{$pid} if $waitpid > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =back | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 11 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # ABSTRACT: Provides a simple, no frills fork manager. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||