1Parallel::Runner(3)   User Contributed Perl Documentation  Parallel::Runner(3)
2
3
4

NAME

6       Parallel::Runner - An object to manage running things in parallel
7       processes.
8

DESCRIPTION

10       There are several other modules to do this, you probably want one of
11       them. This module exists as a super specialised parallel task manager.
12       You create the object with a proces limit and callbacks for what to do
13       while waiting for a free process slot, as well as a callback for what a
14       process shoudl do just before exiting.
15
16       You must explicetly call $runner->finish() when you are done. If the
17       runner is destroyed before it's children are finished a warning will be
18       generated and your child processes will be killed, by force if
19       necessary.
20
21       If you specify a maximum of 1 then no forking will occur, and run()
22       will block until the coderef returns. You can force a fork by providing
23       a boolean true value as the second argument to run(), this will force
24       the runner to fork before running the coderef, however run() will still
25       block until it the child exits.
26

SYNOPSYS

28           #!/usr/bin/perl
29           use strict;
30           use warnings;
31           use Parallel::Runner;
32
33           my $runner = Parallel::Runner->new(4);
34           $runner->run( sub { ... } );
35           $runner->run( sub { ... } );
36           $runner->run( sub { ... } );
37           $runner->run( sub { ... } );
38
39           # This will block until one of the previous 4 finishes
40           $runner->run( sub { ... } );
41
42           # Do not forget this.
43           $runner->finish;
44

CONSTRUCTOR

46       $runner = $class->new( $max, $accessor => $value, ... );
47           Create a new instance of Parallel::Runner. $accessor can be
48           anything listed under the ACCESSORS section. $max should be the
49           maximum number of processes allowed, defaults to 1.
50

ACCESSORS

52       These are simple accessors, provididng an argument sets the accessor to
53       that argument, no argument it simply returns the current value.
54
55       $val = $runner->data_callback( \&callback )
56           If this is specified than IPC will be automatically enabled, and
57           the final return from each process will be passed into this handler
58           in the main process.  Due to the way IPC works only
59           strings/numerical data is passed, if you need to pass a ref you
60           will need to serialize it yourself before returning it, followed by
61           deserializing it in your callback.
62
63           Example:
64
65               # Place to put the accumulated data
66               my @accum_data;
67
68               # Create the runner with a callback that pushes the data onto our array.
69               $runner = $CLASS->new( 2,
70                   data_callback => sub {
71                       my ($data) = @_;
72                       push @accum_data => $data;
73                   },
74               );
75
76               # 4 processes that return data
77               $runner->run( sub { return "foo" });
78               $runner->run( sub { return "bar" });
79               $runner->run( sub { return "baz" });
80               $runner->run( sub { return "bat" });
81               $runner->finish;
82
83               # Verify the data (order is not predictable)
84               is_deeply(
85                   [ sort @accum_data ],
86                   [ sort qw/foo bar baz bat/ ],
87                   "Got all data returned by subprocesses"
88               );
89
90       $val = $runner->exit_callback( \&callback )
91           Codref to call just before a child exits (called within child)
92
93       $val = $runner->iteration_delay( $float );
94           How long to wait per iterate if nothing has changed.
95
96       $val = $runner->iteration_callback( $newval )
97           Coderef to call multiple times in a loop while run() is blocking
98           waiting for a process slot.
99
100       $val = $runner->reap_callback( $newval )
101           Codref to call whenever a pid is reaped using waitpid. The callback
102           sub will be passed 3 values The first is the exit status of the
103           child process. The second is the pid of the child process. The
104           third used to be the return of waitpid, but this is depricated as
105           Child is now used and throws an exception when waitpid is not what
106           it should be. The third is simply the pid of the child process
107           again. The final argument is the child process object itself.
108
109               $runner->reap_callback( sub {
110                   my ( $status, $pid, $pid_again, $proc ) = @_;
111
112                   # Status as returned from system, so 0 is good, 1+ is bad.
113                   die "Child $pid did not exit 0"
114                       if $status;
115               });
116
117       @children = $runner->children( @append )
118           Returns a list of Child::Link::Proc objects.
119
120       $val = $runner->pid()
121           pid of the parent process
122
123       $val = $runner->max( $newval )
124           Maximum number of children
125

OBJECT METHODS

127       run( $code )
128       run( $code, $force_fork )
129           Run the specified code in a child process. Blocks if no free slots
130           are available. Force fork can be used to force a fork when max is
131           1, however it will still block until the child exits.
132
133       finish()
134       finish( $timeout )
135       finish( $timeout, $timeoutcallback )
136           Wait for all children to finish, then clean up after them. If a
137           timeout is specified it will return after the timeout regardless of
138           wether or not children have all exited. If there is a timeout call
139           back then that code will be run upon timeout just before the method
140           returns.
141
142           NOTE: DO NOT LET YOUR RUNNER BE DESTROYED BEFORE FINISH COMPLETES
143           WITHOUT A TIMEOUT.
144
145           the runner will kill all children, possibly with force if your
146           runner is destroyed with children still running, or not waited on.
147
148       killall( $sig )
149           Send all children the specified kill signal.
150
151       DESTROY()
152           Automagically called when the object is destroyed. If called while
153           children are running it will forcefully clean up after you as
154           follows:
155
156           1) Sends an ugly warning.
157
158           2) Will first give all your children 1 second to complete.
159
160           Windows) Strawberry fails with processes, so on windows DESTROY
161           will wait as long as needed, possibly forever.
162
163           3) Sends kill signal 15 to all children then waits up to 4 seconds.
164
165           4) Sends kill signal 9 to any remaining children then waits up to
166           10 seconds
167
168           5) Gives up and returns
169

FENNEC PROJECT

171       This module is part of the Fennec project. See Fennec for more details.
172       Fennec is a project to develop an extendable and powerful testing
173       framework.  Together the tools that make up the Fennec framework
174       provide a potent testing environment.
175
176       The tools provided by Fennec are also useful on their own. Sometimes a
177       tool created for Fennec is useful outside the greator framework. Such
178       tools are turned into their own projects. This is one such project.
179
180       Fennec - The core framework
181         The primary Fennec project that ties them all together.
182

AUTHORS

184       Chad Granum exodist7@gmail.com
185
187       Copyright (C) 2010 Chad Granum
188
189       Parallel-Runner is free software; Standard perl licence.
190
191       Parallel-Runner is distributed in the hope that it will be useful, but
192       WITHOUT ANY WARRANTY; without even the implied warranty of
193       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the license
194       for more details.
195
196
197
198perl v5.28.1                      2019-02-02               Parallel::Runner(3)
Impressum