.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Parallel::Runner 3pm" .TH Parallel::Runner 3pm "2022-11-19" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Parallel::Runner \- An object to manage running things in parallel processes. .SH "DESCRIPTION" .IX Header "DESCRIPTION" There are several other modules to do this, you probably want one of them. This module exists as a super specialised parallel task manager. You create the object with a process limit and callbacks for what to do while waiting for a free process slot, as well as a callback for what a process should do just before exiting. .PP You must explicitly call \f(CW$runner\fR\->\fBfinish()\fR when you are done. If the runner is destroyed before it's children are finished a warning will be generated and your child processes will be killed, by force if necessary. .PP If you specify a maximum of 1 then no forking will occur, and \fBrun()\fR will block until the coderef returns. You can force a fork by providing a boolean true value as the second argument to \fBrun()\fR, this will force the runner to fork before running the coderef, however \fBrun()\fR will still block until it the child exits. .SH "SYNOPSYS" .IX Header "SYNOPSYS" .Vb 4 \& #!/usr/bin/perl \& use strict; \& use warnings; \& use Parallel::Runner; \& \& my $runner = Parallel::Runner\->new(4); \& $runner\->run( sub { ... } ); \& $runner\->run( sub { ... } ); \& $runner\->run( sub { ... } ); \& $runner\->run( sub { ... } ); \& \& # This will block until one of the previous 4 finishes \& $runner\->run( sub { ... } ); \& \& # Do not forget this. \& $runner\->finish; .Ve .SH "CONSTRUCTOR" .IX Header "CONSTRUCTOR" .ie n .IP "$runner = $class\->new( $max, $accessor => $value, ... );" 4 .el .IP "\f(CW$runner\fR = \f(CW$class\fR\->new( \f(CW$max\fR, \f(CW$accessor\fR => \f(CW$value\fR, ... );" 4 .IX Item "$runner = $class->new( $max, $accessor => $value, ... );" Create a new instance of Parallel::Runner. \f(CW$accessor\fR can be anything listed under the \s-1ACCESSORS\s0 section. \f(CW$max\fR should be the maximum number of processes allowed, defaults to 1. .SH "ACCESSORS" .IX Header "ACCESSORS" These are simple accessors, provididng an argument sets the accessor to that argument, no argument it simply returns the current value. .ie n .IP "$val = $runner\->data_callback( \e&callback )" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->data_callback( \e&callback )" 4 .IX Item "$val = $runner->data_callback( &callback )" If this is specified than \s-1IPC\s0 will be automatically enabled, and the final return from each process will be passed into this handler in the main process. Due to the way \s-1IPC\s0 works only strings/numerical data is passed, if you need to pass a ref you will need to serialize it yourself before returning it, followed by deserializing it in your callback. .Sp Example: .Sp .Vb 2 \& # Place to put the accumulated data \& my @accum_data; \& \& # Create the runner with a callback that pushes the data onto our array. \& $runner = $CLASS\->new( 2, \& data_callback => sub { \& my ($data) = @_; \& push @accum_data => $data; \& }, \& ); \& \& # 4 processes that return data \& $runner\->run( sub { return "foo" }); \& $runner\->run( sub { return "bar" }); \& $runner\->run( sub { return "baz" }); \& $runner\->run( sub { return "bat" }); \& $runner\->finish; \& \& # Verify the data (order is not predictable) \& is_deeply( \& [ sort @accum_data ], \& [ sort qw/foo bar baz bat/ ], \& "Got all data returned by subprocesses" \& ); .Ve .ie n .IP "$val = $runner\->exit_callback( \e&callback )" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->exit_callback( \e&callback )" 4 .IX Item "$val = $runner->exit_callback( &callback )" Codref to call just before a child exits (called within child) .ie n .IP "$val = $runner\->iteration_delay( $float );" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->iteration_delay( \f(CW$float\fR );" 4 .IX Item "$val = $runner->iteration_delay( $float );" How long to wait per iterate if nothing has changed. .ie n .IP "$val = $runner\->iteration_callback( $newval )" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->iteration_callback( \f(CW$newval\fR )" 4 .IX Item "$val = $runner->iteration_callback( $newval )" Coderef to call multiple times in a loop while \fBrun()\fR is blocking waiting for a process slot. .ie n .IP "$val = $runner\->reap_callback( $newval )" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->reap_callback( \f(CW$newval\fR )" 4 .IX Item "$val = $runner->reap_callback( $newval )" Codref to call whenever a pid is reaped using waitpid. The callback sub will be passed 3 values The first is the exit status of the child process. The second is the pid of the child process. The third used to be the return of waitpid, but this is depricated as Child is now used and throws an exception when waitpid is not what it should be. The third is simply the pid of the child process again. The final argument is the child process object itself. .Sp .Vb 2 \& $runner\->reap_callback( sub { \& my ( $status, $pid, $pid_again, $proc ) = @_; \& \& # Status as returned from system, so 0 is good, 1+ is bad. \& die "Child $pid did not exit 0" \& if $status; \& }); .Ve .ie n .IP "@children = $runner\->children( @append )" 4 .el .IP "\f(CW@children\fR = \f(CW$runner\fR\->children( \f(CW@append\fR )" 4 .IX Item "@children = $runner->children( @append )" Returns a list of Child::Link::Proc objects. .ie n .IP "$val = $runner\->\fBpid()\fR" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->\fBpid()\fR" 4 .IX Item "$val = $runner->pid()" pid of the parent process .ie n .IP "$val = $runner\->max( $newval )" 4 .el .IP "\f(CW$val\fR = \f(CW$runner\fR\->max( \f(CW$newval\fR )" 4 .IX Item "$val = $runner->max( $newval )" Maximum number of children .SH "OBJECT METHODS" .IX Header "OBJECT METHODS" .ie n .IP "run( $code )" 4 .el .IP "run( \f(CW$code\fR )" 4 .IX Item "run( $code )" .PD 0 .ie n .IP "run( $code, $force_fork )" 4 .el .IP "run( \f(CW$code\fR, \f(CW$force_fork\fR )" 4 .IX Item "run( $code, $force_fork )" .PD Run the specified code in a child process. Blocks if no free slots are available. Force fork can be used to force a fork when max is 1, however it will still block until the child exits. .IP "\fBfinish()\fR" 4 .IX Item "finish()" .PD 0 .ie n .IP "finish( $timeout )" 4 .el .IP "finish( \f(CW$timeout\fR )" 4 .IX Item "finish( $timeout )" .ie n .IP "finish( $timeout, $timeoutcallback )" 4 .el .IP "finish( \f(CW$timeout\fR, \f(CW$timeoutcallback\fR )" 4 .IX Item "finish( $timeout, $timeoutcallback )" .PD Wait for all children to finish, then clean up after them. If a timeout is specified it will return after the timeout regardless of wether or not children have all exited. If there is a timeout call back then that code will be run upon timeout just before the method returns. .Sp \&\s-1NOTE: DO NOT LET YOUR RUNNER BE DESTROYED BEFORE FINISH COMPLETES WITHOUT A TIMEOUT.\s0 .Sp the runner will kill all children, possibly with force if your runner is destroyed with children still running, or not waited on. .ie n .IP "killall( $sig )" 4 .el .IP "killall( \f(CW$sig\fR )" 4 .IX Item "killall( $sig )" Send all children the specified kill signal. .IP "\s-1\fBDESTROY\s0()\fR" 4 .IX Item "DESTROY()" Automagically called when the object is destroyed. If called while children are running it will forcefully clean up after you as follows: .Sp 1) Sends an ugly warning. .Sp 2) Will first give all your children 1 second to complete. .Sp Windows) Strawberry fails with processes, so on windows \s-1DESTROY\s0 will wait as long as needed, possibly forever. .Sp 3) Sends kill signal 15 to all children then waits up to 4 seconds. .Sp 4) Sends kill signal 9 to any remaining children then waits up to 10 seconds .Sp 5) Gives up and returns .SH "FENNEC PROJECT" .IX Header "FENNEC PROJECT" This module is part of the Fennec project. See Fennec for more details. Fennec is a project to develop an extendable and powerful testing framework. Together the tools that make up the Fennec framework provide a potent testing environment. .PP The tools provided by Fennec are also useful on their own. Sometimes a tool created for Fennec is useful outside the greator framework. Such tools are turned into their own projects. This is one such project. .IP "Fennec \- The core framework" 2 .IX Item "Fennec - The core framework" The primary Fennec project that ties them all together. .SH "AUTHORS" .IX Header "AUTHORS" Chad Granum exodist7@gmail.com .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 2010 Chad Granum .PP Parallel-Runner is free software; Standard perl licence. .PP Parallel-Runner is distributed in the hope that it will be useful, but \s-1WITHOUT ANY WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS FOR A PARTICULAR PURPOSE.\s0 See the license for more details.