blib/lib/POE/Wheel/Spawner.pm

filter: Statementstmt 29.6% 19 / 64 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.0% 6 / 15 Podpod 100% 3 / 3 totaltotal 23.7% 28 / 118 CC 16 SCAR 29.0
CC
16
Cov
20%
new
42.0
_handle_sig_child
30.0
run
20.0
CRAP 18.1
↑ Summary
1 package POE::Wheel::Spawner;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use fields qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
8 pool_size
9 stop_if_done
10 workload
11 _workers_sig_count
12 2 /;
13  
14 2 ▶use POE qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
15 Wheel::Run
16 Filter::Reference
17 2 /;
18  
19 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 POE::Wheel::Spawner - A simple subprocess spawner
24  
25 =head1 DESCRIPTION
26  
27 POE::Wheel::Spawner starts on L<run|/run(%arg)> only one subprocess for the L<workload|/new(%arg)>. Until number of subprocesses does not exceed L<pool_size|/new(%arg)> each subprocess can L<spawn|/spawn($pid)> another one.
28  
29 =head1 VERSION
30  
31 Version v0.03.2
32  
33 =cut
34  
35 2 $POE::Wheel::Spawner::VERSION = version->declare("0.03.2");
36  
37 =head1 SYNOPSIS
38  
39 use POE::Wheel::Spawner;
40  
41 my $foo = POE::Wheel::Spawner->new(
42 pool_size => 2,
43 stop_if_done => 1,
44 workload => sub { _workload() }
45 );
46  
47 $foo->run();
48  
49 sub _workload {
50  
51 # request for a new sibling
52 $foo->spawn($$);
53  
54 # ...
55 }
56  
57 =head1 SUBROUTINES/METHODS
58  
59 =head2 new(%arg)
60  
61 Arguments:
62  
63 =over
64  
65 =item
66  
67 pool_size
68  
69 the number of maximal parallel executed C<workload>
70  
71 =item
72  
73 stop_if_done
74  
75 stop after all C<pool_size> pid's are exited.
76  
77 run endless if !C<stop_if_done>
78  
79 =item
80  
81 workload
82  
83 CODE reference to be executed
84  
85 =back
86  
87 =cut
88  
89 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
90 0 my POE::Wheel::Spawner $self = shift;
91 0 my (%opts) = @_;
92 0 ▶ unless (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
93 0 $self = fields::new($self);
94 }
95  
96 0 ▶ if (defined($opts{pool_size})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"pool_size"}) 0 0 0
97 0 ▶ $opts{pool_size} =~ /^\d+$/
Branch: 1 branch
BranchTrueFalseTotal
$opts{"pool_size"} =~ /^\d+$/ or die "'pool_size' property expects a positive integer value" 0 0 0
98 || die "'pool_size' property expects a positive integer value";
99 }
100  
101 0 ▶ $self->{pool_size} = int(delete($opts{pool_size}) || 0);
Condition0%0/2
delete $opts{'pool_size'} || 0
l!l
00
MC/DC0%0/1
delete $opts{'pool_size'} || 0
delete $opts{'pool_size'}
Truth table0%0/2
A
A= delete $opts{'pool_size'}
Aresult
11
00
102  
103 0 $self->{stop_if_done} = delete($opts{stop_if_done});
104 0 $self->{workload} = delete($opts{workload});
105 0 $self->{_workers_sig_count} = 0;
106  
107 0 ▶ %opts && warn sprintf("ignore unsupported properties '%s'", keys(%opts));
Branch: 1 branch
BranchTrueFalseTotal
%opts and warn sprintf("ignore unsupported properties '%s'", keys %opts) 0 0 0
108  
109 0 return $self;
110 } ## end sub new
111  
112 =head2 run(%arg)
113  
114 optional C<%arg> arguments for L<POE::Session>:
115  
116 =over
117  
118 =item
119  
120 debug
121  
122 default 0
123  
124 =item
125  
126 trace
127  
128 default 0
129  
130 =back
131  
132 create a L<POE::Session>
133  
134 run L<POE::Kernel>
135  
136 =cut
137  
138 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
139 0 my ($self, %arg) = @_;
140  
141 0 ▶ ref($self->{workload}) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"workload"} eq "CODE" or die "work_method is not a code reference" 0 0 0
142 || die "work_method is not a code reference";
143  
144 POE::Session->create(
145 0 ▶ options => { debug => $arg{debug} || 0, trace => $arg{trace} || 0 },
Condition0%0/4
$arg{'debug'} || 0
l!l
00
$arg{'trace'} || 0
l!l
00
MC/DC0%0/1
$arg{'debug'} || 0
$arg{'debug'}
MC/DC0%0/1
$arg{'trace'} || 0
$arg{'trace'}
Truth table0%0/2
A
A= $arg{'debug'}
Aresult
11
00
Truth table0%0/2
A
A= $arg{'trace'}
Aresult
11
00
146 object_states => [
147 $self => {
148 _start => '_handle_start',
149 _next => '_handle_start',
150 _sig_child => '_handle_sig_child',
151 _done => '_handle_done',
152 _stderr => '_handle_stderr',
153 _stdout => '_handle_stdout',
154 }
155 ]
156 );
157  
158 0 POE::Kernel->run();
159 } ## end sub run
160  
161 =head2 spawn($pid)
162  
163 request to spawn
164  
165 =cut
166  
167 0 ▶sub spawn {
Subroutine spawn: not called CC=1 SCAR=6.9
Pod documented
168 0 my ($self, $pid) = @_;
169 0 my $filter = POE::Filter::Reference->new();
170 0 my $output = $filter->put([{ busy_worker_pid => $pid }]);
171  
172 0 print @$output;
173 } ## end sub spawn
174  
175 #=head2 _handle_start
176 #
177 #handle C<_start> and C<_next> events defined in POE::Session, which is initialized in C<run>.
178 #
179 #start execution of C<workload> by C<pool_size> parallel running pids
180 #
181 #=cut
182  
183 0 ▶sub _handle_start {
Subroutine _handle_start: not called CC=2 SCAR=17.9
184 0 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
185  
186 0 my $pids_count = scalar(keys(%{ $heap->{worker_by_pid} }));
187 0 ▶ ($pids_count >= $self->{pool_size}) && return;
Branch: 1 branch
BranchTrueFalseTotal
$pids_count >= $self->{"pool_size"} and return 0 0 0
188  
189 my $w = POE::Wheel::Run->new(
190 0 ▶ Program => sub { &{ $self->{workload} } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 StdoutFilter => POE::Filter::Reference->new(),
192 StdoutEvent => "_stdout",
193 StderrEvent => "_stderr",
194 CloseEvent => "_done",
195 );
196  
197 0 $heap->{worker_by_pid}->{ $w->PID } = $w;
198 0 $kernel->sig_child($w->PID, "_sig_child");
199 } ## end sub _handle_start
200  
201 #=head2 _handle_sig_child
202 #
203 #Clear heap. Trigger '_next' if !stop_if_done and currently no child is busy
204 #
205 #=cut
206  
207 0 ▶sub _handle_sig_child {
Subroutine _handle_sig_child: not called CC=5 SCAR=34.0
208 0 my ($self, $kernel, $heap, $pid, $exit_val)
209 = @_[OBJECT, KERNEL, HEAP, ARG1, ARG2];
210  
211 0 ++$self->{_workers_sig_count};
212  
213 0 my $child = delete $heap->{worker_by_pid}{$pid};
214 0 ▶ unless ($child) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child) 0 0 0
215 0 POE::Kernel::_die("no child pid: $pid");
216 }
217  
218 0 delete $heap->{busy_worker_pid}->{$pid};
219  
220 0 ▶ if ($self->{stop_if_done}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'stop_if_done'}) { } 0 0 0
221 0 ▶ ($self->{_workers_sig_count} >= $self->{pool_size}) && return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_workers_sig_count"} >= $self->{"pool_size"} and return 0 0 0
222 }
223 else {
224 0 ▶ (scalar(keys(%{ $heap->{busy_worker_pid} })))
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$$heap{"busy_worker_pid"};} or $kernel->yield("_next") 0 0 0
225 || $kernel->yield("_next");
226 }
227 } ## end sub _handle_sig_child
228  
229 #=head2 _handle_done
230 #
231 #is not implemented yet
232 #
233 #=cut
234  
235 0 ▶sub _handle_done { }
Subroutine _handle_done: not called CC=1 SCAR=0.0
236  
237 #=head2 _handle_stderr
238 #
239 #provide STDERR to POE::Kernel::_warn
240 #
241 #=cut
242  
243 0 ▶sub _handle_stderr {
Subroutine _handle_stderr: not called CC=1 SCAR=6.9
244 0 my ($self, $input, $wheel_id) = @_[OBJECT, ARG0, ARG1];
245 0 POE::Kernel::_warn("wheel $wheel_id STDERR: $input");
246 }
247  
248 #=head2 _handle_stdout
249 #
250 #evaluate from child to stdout printed result.
251 #
252 #trigger _next event if child asks - by using busy_worker_pid printed to stdout - for a sibling
253 #
254 #=cut
255  
256 0 ▶sub _handle_stdout {
Subroutine _handle_stdout: not called CC=3 SCAR=24.8
257 0 my ($self, $kernel, $heap, $result) = @_[OBJECT, KERNEL, HEAP, ARG0];
258 0 ▶ if (ref($result) eq 'HASH' && $result->{busy_worker_pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH" and $result->{"busy_worker_pid"}) 0 0 0
Condition0%0/3
ref $result eq "HASH" and $result->{"busy_worker_pid"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result eq "HASH" and $result->{"busy_worker_pid"}
ref $result eq "HASH" $result->{"busy_worker_pid"}
Truth table0%0/3
A and B
A= ref $result eq "HASH"B= $result->{"busy_worker_pid"}
ABresult
0X0
100
111
259 0 $heap->{busy_worker_pid}->{ $result->{busy_worker_pid} } = 1;
260 0 $kernel->yield("_next");
261 }
262 } ## end sub _handle_stdout
263  
264 2 1; # End of POE::Wheel::Spawner
265  
266 =head1 AUTHOR
267  
268 Alexei Pastuchov E<lt>palik at cpan.orgE<gt>.
269  
270 =head1 REPOSITORY
271  
272 L<https://github.com/p-alik/POE-Wheel-Spawner.git>
273  
274 =head1 LICENSE AND COPYRIGHT
275  
276  
277 Copyright 2014-2016 by Alexei Pastuchov E<lt>palik at cpan.orgE<gt>.
278  
279 This library is free software; you can redistribute it and/or modify
280 it under the same terms as Perl itself.
281  
282 =cut
↑ Summary