Quiq/Parallel.pm

filter: Statementstmt 83.6% 87 / 104 Branchbran 50.0% 17 / 34 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 73.3% 124 / 169 CC 22 SCAR 30.1
CC
22
Cov
73%
runFetch
35.9
__ANON__
4.4
BEGIN
1.0
CRAP 20.3
« Quiq/Html/Widget/CheckBox.pm ↑ Summary Quiq/Sendmail.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Parallel - Parallele Verarbeitung
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 EXAMPLE
14  
15 Minimales Veranschaulichungsbeispiel: Lasse 50 Prozesse für
16 jeweils eine Sekunde schlafen. Die Ausführungsdauer beträgt
17 ungefähr 50/I<Anzahl CPUs> Sekunden, da immer I<Anzahl CPUs>
18 Prozesse parallel ausgeführt werden.
19  
20 Quiq::Parallel->runArray([1..50],sub {
21 my ($elem,$i) = @_;
22 sleep 1;
23 return;
24 });
25  
26 Bei großen Datenmengen oder wenn die Gesamtmenge vorab nicht bekannt
27 ist, bietet sich die Methode $class->L<runFetch|"runFetch() - Führe Subroutine parallel über gefetchten Elementen aus">() an. Hier ein
28 Beispiel mit einer unbekannt großen Datenbank-Selektion:
29  
30 my $cur = $db->select("
31 <SELECT Statement>
32 ",
33 -cursor => 1,
34 );
35
36 Quiq::Parallel->runFetch(sub {
37 my $i = shift;
38 return $cur->fetch;
39 },
40 sub {
41 my ($row,$i) = @_;
42
43 <$row verarbeiten>
44
45 return;
46 },
47 );
48  
49 =cut
50  
51 # -----------------------------------------------------------------------------
52  
53 package Quiq::Parallel;
54 11 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 11 our $VERSION = '1.240';
61  
62 11 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 11 ▶use Quiq::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 11 ▶use Quiq::Progress;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 11 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 11 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 11 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 # -----------------------------------------------------------------------------
70  
71 =head1 METHODS
72  
73 =head2 Parallele Berechnung
74  
75 =head3 runArray() - Führe Subroutine parallel über Arrayelementen aus
76  
77 =head4 Synopsis
78  
79 $class->runArray(\@elements,$sub,@opt);
80  
81 =head4 Arguments
82  
83 =over 4
84  
85 =item @elements
86  
87 Die Elemente, auf denen die Berechnung einzeln durchgeführt wird.
88  
89 =item $sub
90  
91 Die Subroutine, die für jedes Element in @elements ausgeführt wird.
92  
93 =back
94  
95 =head4 Options
96  
97 Die gleichen wie L<runFetch|"runFetch() - Führe Subroutine parallel über gefetchten Elementen aus">().
98  
99 =head4 Returns
100  
101 nichts
102  
103 =cut
104  
105 # -----------------------------------------------------------------------------
106  
107 11 ▶sub runArray {
Subroutine runArray: called CC=1 SCAR=0.0
Pod documented
108 11 my ($class,$elementA,$sub) = splice @_,0,3;
109 # @_: @opt
110  
111 65 ▶ $class->runFetch(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 65 my $i = shift;
113 65 return $elementA->[$i-1];
114 },
115 11 $sub,
116 -maxFetches => scalar @$elementA,
117 @_,
118 );
119
120 1 return;
121 }
122  
123 # -----------------------------------------------------------------------------
124  
125 =head3 runFetch() - Führe Subroutine parallel über gefetchten Elementen aus
126  
127 =head4 Synopsis
128  
129 $class->runFetch($fetchSub,$execSub,@opt);
130  
131 =head4 Arguments
132  
133 =over 4
134  
135 =item $fetchSub
136  
137 Subroutine, die das nächste gefetchte Element liefert:
138  
139 $e = $fetchSub->($i); # $i-ter Fetch-Aufruf
140  
141 =item $execSub
142  
143 Subroutine, die für jedes gefetchte Element ausgeführt wird.
144  
145 =back
146  
147 =head4 Options
148  
149 =over 4
150  
151 =item -maxFetches => $n (Default: 0)
152  
153 Gesamtanzahl der Fetches. 0 bedeutet, die Gesamtanzahl der Fetches
154 ist (vorab) nicht bekannt.
155  
156 =item -maxProcesses => $n (Default: Anzahl der CPUs des Rechners)
157  
158 Die maximale Anzahl parallel laufender Prozesse.
159  
160 =item -outputDir => $dir (Default: undef)
161  
162 Verzeichnis, in das die Ausgaben der Prozesse auf STDOUT und STDERR
163 geschrieben werden, jeweils in eine eigene Datei mit dem Namen
164  
165 NNNNNN.out
166  
167 Die sechstellige Zahl NNNNNNN ist die Nummer des Prozesses in der
168 Aufrufreihenfolge.
169  
170 =item -outputFile => $file (Default: undef)
171  
172 Datei, in der die Ausgaben aller Prozesse (chronologische
173 Aufrufreihenfolge) zusammengefasst werden. Dies geschieht nach
174 Beendigung des letzten Prozesses. Wird '-' als Dateiname angegeben,
175 wird die Ausgabe nach STDOUT geschrieben. ACHTUNG: Bricht das
176 steuernde Programm ab, wird keine Ausgabe produziert. Die
177 Ausgabedateien verschwinden mit dem temporären Verzeichnis,
178 in dem sie gespeichert werden.
179  
180 =item -progressMeter => $bool (Default: 1)
181  
182 Zeige Fortschrittsanzeige an.
183  
184 =item -progressNameSub => sub {}
185  
186 Liefere die Bezeichnung des Elements für die Fortschrittsanzeige.
187 Dies ist erforderlich, wenn das Element ein Objekt ist. Beispiel:
188  
189 -progressNameSub => sub {
190 my $obj = shift;
191 return $obj->bezeichnung;
192 },
193  
194 =back
195  
196 =head4 Returns
197  
198 nichts
199  
200 =head4 Description
201  
202 Verarbeite die Elemente, die von Subroutine $fetchSub geliefert
203 werden, mit der Subroutine $execSub mit parallel laufenden
204 Prozessen. Per Default wird für die Anzahl der parallelen Prozesse
205 die Anzahl der CPUs des ausführenden Rechners gewählt. Mit
206 der Option -maxProcesses kann eine abweichende Anzahl gewählt
207 werden.
208  
209 Tipp: Die Anzahl der vorhandenen CPUs liefert die Methode
210  
211 $n = Quiq::System->numberOfCpus;
212  
213 Die Ausgaben der Prozesse auf STDOUT und STDERR werden in Dateien
214 gespeichert, wenn Option -outputDir und/oder -outputFile angegeben sind.
215  
216 =cut
217  
218 # -----------------------------------------------------------------------------
219  
220 11 ▶sub runFetch {
Subroutine runFetch: called CC=19 SCAR=35.8
Pod documented
221 11 my $class = shift;
222 # @_: Parameters
223  
224 # Pfadobjekt
225 11 my $p = Quiq::Path->new;
226  
227 # Optionen
228  
229 11 my $maxFetches = 0;
230 11 my $maxProcesses = 0;
231 11 my $outputDir = undef;
232 11 my $outputFile = undef;
233 11 my $progressMeter = 1;
234 11 my $progressNameSub = undef;
235
236 11 my $argA = $class->parameters(2,2,\@_,
237 -maxFetches => \$maxFetches,
238 -maxProcesses => \$maxProcesses,
239 -outputDir => \$outputDir,
240 -outputFile => \$outputFile,
241 -progressMeter => \$progressMeter,
242 -progressNameSub => \$progressNameSub,
243 );
244 11 my ($fetchSub,$sub) = @$argA;
245  
246 11 ▶ if (!$maxProcesses) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($maxProcesses) 11 0 11
247 11 $maxProcesses = Quiq::System->numberOfCpus;
248 }
249  
250 11 my $dir;
251 11 ▶ if ($outputDir || $outputFile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputDir or $outputFile) 0 11 11
Condition33%1/3
$outputDir or $outputFile
l!l&&r!l&&!r
0011
MC/DC0%0/2
$outputDir or $outputFile
$outputDir $outputFile
Truth table33%1/3
A or B
A= $outputDirB= $outputFile
ABresult
1X1
011
000
252 # Die Prozesse schreiben ihre Ausgabe auf stdout
253 # und stderr nach $dir
254  
255 0 ▶ if ($outputDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputDir) 0 0 0
256 0 $p->mkdir($outputDir,-recursive=>1);
257 }
258 0 ▶ $dir = $outputDir || $p->tempDir;
Condition0%0/3
$outputDir || $p->tempDir
l!l&&r!l&&!r
000
MC/DC0%0/2
$outputDir || $p->tempDir
$outputDir $p->tempDir
Truth table0%0/3
A || B
A= $outputDirB= $p->tempDir
ABresult
1X1
011
000
259 }
260  
261 # Aktionen nach Beendigung eines Child-Prozesses
262  
263 11 ▶ my $wait = sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
264 11 my ($pro,$processH,$i) = @_;
265  
266 11 my $pid = wait;
267 11 ▶ if ($pid >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid >= 0) 10 1 11
268 10 my $elem = $processH->{$pid};
269  
270 10 my $type = Scalar::Util::reftype($elem);
271 10 ▶ if (!$type) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 10 0 10
elsif ($progressNameSub) { } 0 0 0
272 10 print $pro->msg($i,'i/n x% t/t(t) x/h x/s: %s',$elem);
273 }
274 elsif ($progressNameSub) {
275 0 print $pro->msg($i,'i/n x% t/t(t) x/h x/s: %s',
276 $progressNameSub->($elem));
277 }
278 else {
279 0 print $pro->msg($i,'i/n x% t/t(t) x/h x/s');
280 }
281  
282 10 delete $processH->{$pid};
283 }
284  
285 11 return $pid;
286 11 };
287  
288 # Ausführung
289  
290 11 my $pro = Quiq::Progress->new($maxFetches,-show=>$progressMeter);
291 11 ▶ if ($maxFetches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxFetches) 11 0 11
292 11 my $msg = 'Waiting for first process to finish';
293 11 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 11 11
294 0 $msg .= " (see $dir for output files)";
295 }
296 11 $msg .= '...';
297 11 print $pro->msg($msg);
298 }
299  
300 11 my $i = 0; # Anzahl der gestarteten Prozesse
301 11 my $j = 0; # Anzahl der beendeten Prozesse
302 11 my $processH = Quiq::Hash->new;
303 11 my $runningProcesses = 0;
304 11 while (1) {
305 66 $i++;
306 66 ▶ if ($maxFetches && $i > $maxFetches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxFetches and $i > $maxFetches) 1 65 66
Condition66%2/3
$maxFetches and $i > $maxFetches
!ll&&!rl&&r
0651
MC/DC50%1/2
$maxFetches and $i > $maxFetches
$maxFetches $i > $maxFetches
Truth table66%2/3
A and B
A= $maxFetchesB= $i > $maxFetches
ABresult
0X0
100
111
307 1 last;
308 }
309 65 my $elem = $fetchSub->($i);
310 65 ▶ if (!defined $elem) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $elem) 0 65 65
311 0 last;
312 }
313  
314 65 ▶ if ($runningProcesses >= $maxProcesses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runningProcesses >= $maxProcesses) 0 65 65
315 0 $wait->($pro,$processH,++$j);
316 0 $runningProcesses--;
317 }
318  
319 65 ▶ if (my $pid = fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 55 10 65
320 # Parent
321  
322 55 $processH->add($pid=>$elem);
323 55 $runningProcesses++;
324 }
325 else {
326 # Child
327  
328 10 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 10 10
329 # Lenke stdout und stderr in Ausgabedateien in Verzeichnis
330 # $dir um. Die Umlenkung gilt auch für Child-Prozesse.
331  
332 0 my $file = $p->expandTilde(sprintf '%s/%06d.out',$dir,$i);
333  
334 0 CORE::close STDOUT;
335 0 ▶ CORE::open STDOUT,'>',$file or $class->throw;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $file or $class->throw 0 0 0
336 0 ▶ CORE::open STDERR,'>&',\*STDOUT or $class->throw;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", \*STDOUT or $class->throw 0 0 0
337 }
338  
339 10 $sub->($elem,$i);
340 10 exit;
341 }
342 }
343  
344 # Warte auf die letzten Childs
345  
346 1 ▶ while ($wait->($pro,$processH,++$j) >= 0) {}
Branch: 1 branch
BranchTrueFalseTotal
if (&$wait($pro, $processH, ++$j) >= 0) 10 1 11
347 1 print $pro->msg;
348  
349 1 ▶ if ($outputFile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputFile) 0 1 1
350 # Die Ausgaben aller Prozesse in Zieldatei schreiben
351  
352 0 my $fh = Quiq::FileHandle->new('>',$outputFile);
353 0 for my $file ($p->glob("$dir/*.out")) {
354 0 $fh->print($p->read($file));
355 }
356 0 $fh->close;
357 }
358
359 1 return;
360 }
361  
362 # -----------------------------------------------------------------------------
363  
364 =head1 VERSION
365  
366 1.240
367  
368 =head1 AUTHOR
369  
370 Frank Seitz, L<http://fseitz.de/>
371  
372 =head1 COPYRIGHT
373  
374 Copyright (C) 2026 Frank Seitz
375  
376 =head1 LICENSE
377  
378 This code is free software; you can redistribute it and/or modify
379 it under the same terms as Perl itself.
380  
381 =cut
382  
383 # -----------------------------------------------------------------------------
384  
385 11 1;
386  
387 # eof
« Quiq/Html/Widget/CheckBox.pm ↑ Summary Quiq/Sendmail.pm »