/usr/share/perl5/Parallel/Runner.pm is in libparallel-runner-perl 0.013-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | package Parallel::Runner;
use strict;
use warnings;
use POSIX ();
use Time::HiRes qw/sleep/;
use Carp;
use Child qw/child/;
our $VERSION = '0.013';
for my $accessor (qw/ exit_callback data_callback iteration_callback _children pid max iteration_delay reap_callback pipe/) {
my $sub = sub {
my $self = shift;
( $self->{$accessor} ) = @_ if @_;
return $self->{$accessor};
};
no strict 'refs';
*$accessor = $sub;
}
sub children {
my $self = shift;
my @active;
for my $proc ( @{$self->_children || []}, @_ ) {
if ( defined $proc->exit_status ) {
if ( $self->data_callback ) {
my $data = $proc->read();
$self->data_callback->($data);
}
$self->reap_callback->( $proc->exit_status, $proc->pid, $proc->pid, $proc )
if $self->reap_callback;
next;
}
push @active => $proc;
}
$self->_children( \@active );
return @active;
}
sub new {
my $class = shift;
my ($max) = shift;
return bless(
{
_children => [],
pid => $$,
max => $max || 1,
iteration_delay => 0.1,
@_,
},
$class
);
}
sub run {
my $self = shift;
my ( $code, $force_fork ) = @_;
croak("Called run() in child process")
unless $self->pid == $$;
my $fork = $force_fork || $self->max > 1;
return $self->_fork($code)
if $fork;
my ($data) = $code->();
$self->data_callback->($data)
if $self->data_callback;
return;
}
sub _fork {
my $self = shift;
my ($code) = @_;
# Wait for a slot
$self->_iterate(
sub {
$self->children >= $self->max;
}
);
my $proc = Child->new(
sub {
my $parent = shift;
$self->_children( [] );
my @return = $code->($parent);
$self->exit_callback->(@return)
if $self->exit_callback;
$parent->write( $return[0] )
if $self->data_callback;
},
$self->pipe || $self->data_callback ? ( pipe => $self->pipe ) : ()
)->start();
$self->_iterate( sub { !defined $proc->exit_status } )
if $self->max == 1;
$self->children($proc);
return $proc;
}
sub finish {
my $self = shift;
$self->_iterate( sub { $self->children }, @_ );
}
sub _iterate {
my $self = shift;
my ( $condition, $timeout, $timeoutsub ) = @_;
my $counter = 0;
while ( $condition->() ) {
$self->iteration_callback->($self)
if $self->iteration_callback;
$counter += $self->iteration_delay;
last if $timeout and $counter >= $timeout;
sleep $self->iteration_delay;
}
$timeoutsub->()
if $timeout
&& $timeoutsub
&& $counter >= $timeout;
1;
}
sub killall {
my $self = shift;
my ( $sig, $warn ) = @_;
if ($warn) {
warn time . " - Killing: $_ - $sig\n" for grep { $_->pid } $self->children;
}
$_->kill($sig) for $self->children;
}
sub DESTROY {
my $self = shift;
return
unless $self->pid == $$
&& $self->children;
warn <<EOT;
Parallel::Runner object destroyed without first calling finish(), This will
terminate all your child processes. This either means you forgot to call
finish() or your parent process has died.
EOT
return $self->finish()
if $^O eq 'MSWin32';
$self->finish(
1,
sub {
$self->killall( 15, 1 );
$self->finish(
4,
sub {
$self->killall( 9, 1 );
$self->finish(10);
}
);
}
);
}
1;
=pod
=head1 NAME
Parallel::Runner - An object to manage running things in parallel processes.
=head1 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.
You must explicitly call $runner->finish() 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.
If you specify a maximum of 1 then no forking will occur, and run() will block
until the coderef returns. You can force a fork by providing a boolean true
value as the second argument to run(), this will force the runner to fork
before running the coderef, however run() will still block until it the child
exits.
=head1 SYNOPSYS
#!/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;
=head1 CONSTRUCTOR
=over 4
=item $runner = $class->new( $max, $accessor => $value, ... );
Create a new instance of Parallel::Runner. $accessor can be anything listed
under the ACCESSORS section. $max should be the maximum number of processes
allowed, defaults to 1.
=back
=head1 ACCESSORS
These are simple accessors, provididng an argument sets the accessor to that
argument, no argument it simply returns the current value.
=over 4
=item $val = $runner->data_callback( \&callback )
If this is specified than IPC 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 IPC 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.
Example:
# 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"
);
=item $val = $runner->exit_callback( \&callback )
Codref to call just before a child exits (called within child)
=item $val = $runner->iteration_delay( $float );
How long to wait per iterate if nothing has changed.
=item $val = $runner->iteration_callback( $newval )
Coderef to call multiple times in a loop while run() is blocking waiting for a
process slot.
=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 L<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.
$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;
});
=item @children = $runner->children( @append )
Returns a list of L<Child::Link::Proc> objects.
=item $val = $runner->pid()
pid of the parent process
=item $val = $runner->max( $newval )
Maximum number of children
=back
=head1 OBJECT METHODS
=over 4
=item run( $code )
=item run( $code, $force_fork )
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.
=item finish()
=item finish( $timeout )
=item finish( $timeout, $timeoutcallback )
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.
NOTE: DO NOT LET YOUR RUNNER BE DESTROYED BEFORE FINISH COMPLETES WITHOUT A
TIMEOUT.
the runner will kill all children, possibly with force if your runner is
destroyed with children still running, or not waited on.
=item killall( $sig )
Send all children the specified kill signal.
=item DESTROY()
Automagically called when the object is destroyed. If called while children are
running it will forcefully clean up after you as follows:
1) Sends an ugly warning.
2) Will first give all your children 1 second to complete.
Windows) Strawberry fails with processes, so on windows DESTROY will wait as
long as needed, possibly forever.
3) Sends kill signal 15 to all children then waits up to 4 seconds.
4) Sends kill signal 9 to any remaining children then waits up to 10 seconds
5) Gives up and returns
=back
=head1 FENNEC PROJECT
This module is part of the Fennec project. See L<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.
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.
=over 2
=item L<Fennec> - The core framework
The primary Fennec project that ties them all together.
=back
=head1 AUTHORS
Chad Granum L<exodist7@gmail.com>
=head1 COPYRIGHT
Copyright (C) 2010 Chad Granum
Parallel-Runner is free software; Standard perl licence.
Parallel-Runner is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
|