Noss/Forker.pm

filter: Statementstmt 93.3% 70 / 75 Branchbran 66.6% 20 / 30 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 85.3% 111 / 130 CC 18 SCAR 11.7
CC
18
Cov
84%
finish
4.5
wait_next
4.0
DESTROY
4.0
CRAP 3.2
« Noss/BaseConfig.pm ↑ Summary Noss/GroupConfig.pm »
1 package WWW::Noss::Forker;
2 112 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 112 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 112 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 112 our $VERSION = '2.05';
6  
7 112 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 112 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 112 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 780 ▶sub new {
Subroutine new: called CC=3 SCAR=13.3
Pod documented
12  
13 780 my ($class, $procs) = @_;
14  
15 780 ▶ if ($procs < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($procs < 0) { } 0 780 780
elsif ($procs == 0) { } 0 780 780
16 0 die '$procs must be >=0';
17 } elsif ($procs == 0) {
18 0 $procs = 1;
19 }
20  
21 780 return bless {
22 Limit => $procs,
23 Finish => undef,
24 Tmp => tempdir(CLEANUP => 0),
25 Procs => {},
26 Num => 0,
27 Child => 0,
28 }, $class;
29  
30 }
31  
32 780 ▶sub run_on_finish {
Subroutine run_on_finish: called CC=2 SCAR=7.6
Pod documented
33  
34 780 my ($self, $sub) = @_;
35  
36 780 ▶ if (ref $sub ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub ne "CODE") 0 780 780
37 0 die '$sub is not a subroutine reference';
38 }
39  
40 780 $self->{ Finish } = $sub;
41  
42 }
43  
44 6280 ▶sub wait_next {
Subroutine wait_next: called CC=4 SCAR=14.0
Pod documented
45  
46 6280 my ($self) = @_;
47  
48 6280 my $w = wait;
49 6280 ▶ if ($w == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w == -1) 670 5610 6280
50 670 return -1;
51 }
52  
53 5610 ▶ if (defined $self->{ Finish }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Finish"}) 5610 0 5610
54 $self->{ Finish }->(
55 $w, $?,
56 5610 ▶ -s $self->{ Procs }{ $w } ? retrieve($self->{ Procs }{ $w }) : undef
Branch: 1 branch
BranchTrueFalseTotal
-s $self->{'Procs'}{$w} ? : 5610 0 5610
57 );
58 }
59  
60 5610 $self->{ Num }--;
61 5610 delete $self->{ Procs }{ $w };
62  
63 5610 return $w;
64  
65 }
66  
67 6215 ▶sub start {
Subroutine start: called CC=4 SCAR=13.9
Pod documented
68  
69 6215 my ($self) = @_;
70  
71 6215 ▶ while ($self->{ Num } >= $self->{ Limit }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Num"} >= $self->{"Limit"}) 2915 6215 9130
72 2915 $self->wait_next;
73 }
74  
75 6215 my $tmp = do {
76 6215 my ($h, $p) = tempfile(UNLINK => 0, DIR => $self->{ Tmp });
77 6215 close $h;
78 6215 $p
79 };
80  
81 6215 my $pid = fork;
82 6215 ▶ if (not defined $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 6215 6215
elsif ($pid) { } 6105 110 6215
83 0 die "error forking: $!\n";
84 } elsif ($pid) {
85 6105 $self->{ Procs }{ $pid } = $tmp;
86 } else {
87 110 $self->{ Procs }{ $$ } = $tmp;
88 110 $self->{ Child } = 1;
89 }
90  
91 6215 $self->{ Num }++;
92  
93 6215 return $pid;
94  
95 }
96  
97 110 ▶sub finish {
Subroutine finish: called CC=4 SCAR=15.0
Pod documented
98  
99 110 my ($self, $code, $data) = @_;
100 110 ▶ $code //= 0;
Condition50%1/2
$code //= 0
l!l
1100
MC/DC0%0/1
$code //= 0
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
101  
102 110 ▶ if (!$self->{ Child }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Child"}) 0 110 110
103 0 die "cannot call finish on non-child process";
104 }
105  
106 110 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 110 0 110
107 110 store($data, $self->{ Procs }{ $$ });
108 }
109 110 exit $code;
110  
111 }
112  
113 670 ▶sub wait_all_children {
Subroutine wait_all_children: called CC=3 SCAR=11.0
Pod documented
114  
115 670 my ($self) = @_;
116  
117 670 my $waited = 0;
118  
119 670 ▶ while ($self->{ Num } > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Num"} > 0) 2695 670 3365
120 2695 my $w = $self->wait_next;
121 2695 ▶ if ($w != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w != -1) 2695 0 2695
122 2695 $waited++;
123 }
124 }
125  
126 670 return $waited;
127  
128 }
129  
130 780 ▶DESTROY {
Subroutine DESTROY: called CC=4 SCAR=14.0
131  
132 780 my ($self) = @_;
133  
134 780 ▶ if ($self->{ Child }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Child"}) 110 670 780
135 110 return;
136 }
137  
138 670 while (1) {
139 670 ▶ my $w = eval { $self->wait_next } // 0;
Condition50%1/2
eval { do { $self->wait_next } } // 0
l!l
6700
MC/DC0%0/1
eval { do { $self->wait_next } } // 0
eval { do { $self->wait_next } }
Truth table50%1/2
A
A= eval { do { $self->wait_next } }
Aresult
11
00
140 670 ▶ last if $w == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $w == -1 670 0 670
141 }
142  
143 670 eval { remove_tree($self->{ Tmp }, { safe => 1 }) };
144  
145 }
146  
147 112 1;
148  
149 =head1 NAME
150  
151 WWW::Noss::Forker - noss fork manager
152  
153 =head1 SYNOPSIS
154  
155 use WWW::Noss::Forker;
156  
157 my $f = WWW::Noss::Forker->new(10);
158  
159 FORKLOOP: for my $dl (@downloads) {
160 $f->start and next FORKLOOP;
161 download($dl);
162 $f->finish;
163 }
164  
165 $f->wait_all_children;
166  
167 =head1 DESCRIPTION
168  
169 B<WWW::Noss::Forker> is a module for managing multiple simultaneous forks
170 easily. It is designed to be a lightweight counterpart to
171 L<Parallel::ForkManager>. This is a private module, please consult the L<noss>
172 manual for user documentation.
173  
174 =head1 METHODS
175  
176 =over 4
177  
178 =item $forker = WWW::Noss::Forker->new($forks)
179  
180 Create a B<WWW::Noss::Forker> object. C<$forks> is the maximum number of
181 forks to perform.
182  
183 =item $forker->run_on_finish($subref)
184  
185 Run C<$subref> upon the exit of a fork. C<$subref> will be given the
186 following arguments when called:
187  
188 0 $pid PID of terminating process
189 1 $exit Exit status of terminating process
190 2 $data Data passed to $forker->finish(), undef if not present
191  
192 =item $kid = $forker->wait_next()
193  
194 Waits for next child to finish running and returns its PID.
195  
196 =item $pid = $forker->start()
197  
198 Starts executing next fork. If the maximum number of forks has been reached,
199 C<$forker> will wait until another fork has been completed.
200  
201 Returns the PID of the forked process to the parent, and C<0> to the fork.
202  
203 =item $forker->finish([ $code, [ $data ] ])
204  
205 Finish and exit the current fork. C<$code> is the code to exit the fork with,
206 defaults to C<0>. C<$data> is a reference to data to pass to the subroutine
207 set in C<run_on_finish()>.
208  
209 =item $waited = $forker->wait_all_children()
210  
211 Waits for all children to complete. Returns the number of children completed.
212  
213 =back
214  
215 =head1 AUTHOR
216  
217 Written by Samuel Young, E<lt>samyoung@posteo.comE<gt>.
218  
219 This project's source can be found on its
220 L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
221 requests are welcome!
222  
223 =head1 COPYRIGHT
224  
225 Copyright (C) 2026 Samuel Young
226  
227 This program is free software: you can redistribute it and/or modify
228 it under the terms of the GNU General Public License as published by
229 the Free Software Foundation, either version 3 of the License, or
230 (at your option) any later version.
231  
232 =head1 SEE ALSO
233  
234 L<Parallel::ForkManager>, L<noss>
235  
236 =cut
« Noss/BaseConfig.pm ↑ Summary Noss/GroupConfig.pm »