Tiny.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 75.0% 18 / 24 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 93.7% 15 / 16 Podpod 100% 2 / 2 totaltotal 84.2% 118 / 140 CC 21 SCAR 17.1
CC
21
Cov
88%
new
11.4
run
7.1
_waitqueue
5.0
CRAP 5.5
↑ Summary Tiny/Test.pm »
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
BranchTrueFalseTotal
ref $_[0] ? : 0 24 24
79  
80 # set some defaults
81 24 ▶ $args->{reap_timeout} ||= DEFAULT_REAP_TIMEOUT;
Condition50%1/2
$args->{"reap_timeout"} ||= "0.1"
l!l
024
MC/DC0%0/1
$args->{"reap_timeout"} ||= "0.1"
$args->{"reap_timeout"}
Truth table50%1/2
A
A= $args->{"reap_timeout"}
Aresult
11
00
82 24 ▶ $args->{subname} ||= DEFAULT_SUBNAME;
Condition50%1/2
$args->{"subname"} ||= "run"
l!l
024
MC/DC0%0/1
$args->{"subname"} ||= "run"
$args->{"subname"}
Truth table50%1/2
A
A= $args->{"subname"}
Aresult
11
00
83 24 ▶ $args->{workers} ||= DEFAULT_WORKERS;
Condition50%1/2
$args->{"workers"} ||= 1
l!l
240
MC/DC0%0/1
$args->{"workers"} ||= 1
$args->{"workers"}
Truth table50%1/2
A
A= $args->{"workers"}
Aresult
11
00
84 24 ▶ $args->{worker_total} ||= DEFAULT_WORKER_TOTAL;
Condition50%1/2
$args->{"worker_total"} ||= 1
l!l
240
MC/DC0%0/1
$args->{"worker_total"} ||= 1
$args->{"worker_total"}
Truth table50%1/2
A
A= $args->{"worker_total"}
Aresult
11
00
85  
86 # special configuration
87 24 ▶ undef $args->{worker_total} if $args->{worker_total} eq 'infinite';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"worker_total"} eq "infinite" 0 24 24
88  
89 # check args
90 24 ▶ die 'no handler provided' unless $args->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"handler"} 0 24 24
91 die "handler doesn't implement $args->{subname}()"
92 24 ▶ unless $args->{handler}->can( $args->{subname} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"handler"}->can($args->{"subname"}) 0 24 24
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
BranchTrueFalseTotal
if ($self->_waitqueue) 65 11 76
124  
125 # parent work
126 65 my $pid = fork();
127 65 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 55 10 65
128 $self->{_worker_total}--
129 55 ▶ if defined $self->{_worker_total} and $self->{_worker_total} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_worker_total"} and $self->{"_worker_total"} > 0 55 0 55
Condition33%1/3
defined $self->{"_worker_total"} and $self->{"_worker_total"} > 0
!ll&&!rl&&r
0055
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (wait != -1) 22 11 33
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
BranchTrueFalseTotal
if defined $self->{"_worker_total"} and $self->{"_worker_total"} <= 0 11 65 76
Condition66%2/3
defined $self->{"_worker_total"} and $self->{"_worker_total"} <= 0
!ll&&!rl&&r
06511
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
ABresult
0X0
100
111
160  
161 # wait to reap at least one child
162 65 ▶ while ( keys %{ $self->{_jobs} } >= $self->{_workers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"_jobs"};} >= $self->{"_workers"}) 82985 65 83050
163 82985 ▶ return 0 unless $self->{_continue};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_continue"} 0 82985 82985
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
BranchTrueFalseTotal
if $waitpid > 0 26 165944 165970
177 }
178 }
179  
180 =back
181  
182 =cut
183  
184 11 1;
185  
186 # ABSTRACT: Provides a simple, no frills fork manager.
187  
↑ Summary Tiny/Test.pm »