| 1 | # See copyright, etc in below POD section. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package Parallel::Forker; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 34 | require 5.006; | |||||||||||||||||||||||||||||||||||||||||
| 6 | 34 | ▶ | use Carp qw(carp croak confess); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 34 | ▶ | use Time::HiRes qw(usleep); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use Parallel::Forker::Process; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 34 | ▶ | use vars qw($Debug $VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 34 | $VERSION = '1.258'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #### CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 53 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 53 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 22 | _activity => 1, # Optionally set true when a sig_child comes | ||||||||||||||||||||||||||||||||||||||||||
| 23 | _processes => {}, # All process objects, keyed by id | ||||||||||||||||||||||||||||||||||||||||||
| 24 | _labels => {}, # List of process objects, keyed by label | ||||||||||||||||||||||||||||||||||||||||||
| 25 | _runable => {}, # Process objects runable now, keyed by id | ||||||||||||||||||||||||||||||||||||||||||
| 26 | _running => {}, # Process objects running now, keyed *PID* | ||||||||||||||||||||||||||||||||||||||||||
| 27 | _run_after_eqn => undef,# Equation to eval to determine if ready to launch | ||||||||||||||||||||||||||||||||||||||||||
| 28 | _parent_pid => $$, # PID of initial process creating the forker | ||||||||||||||||||||||||||||||||||||||||||
| 29 | max_proc => undef, # Number processes to launch, <1=any, +=that number | ||||||||||||||||||||||||||||||||||||||||||
| 30 | use_sig_child => undef, # Default to not using SIGCHLD handler | ||||||||||||||||||||||||||||||||||||||||||
| 31 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 53 | ▶ | bless $self, ref($class)||$class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 53 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | #### ACCESSORS | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | sub in_parent { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | return $self->{_parent_pid}==$$; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | sub max_proc { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
max_proc: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | ▶ | $self->{max_proc} = shift if $#_>=0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | return $self->{max_proc}; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | sub use_sig_child { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 976 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
use_sig_child: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 976 | ▶ | $self->{use_sig_child} = shift if $#_>=0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 976 | return $self->{use_sig_child}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | sub running { | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
running: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 23 | return (values %{$self->{_running}}); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | sub running_sorted { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
running_sorted: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return (sort {$a->{name} cmp $b->{name}} values %{$self->{_running}}); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | sub process { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | confess "usage: \$fork->process(\$name)" unless scalar(@_) == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return $self->{_processes}{$_[0]}; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | sub processes { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 52 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
processes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 52 | return (values %{$self->{_processes}}); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | sub processes_sorted { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 42 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
processes_sorted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 42 | return (sort {$a->{name} cmp $b->{name}} values %{$self->{_processes}}); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | sub state_stats { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
state_stats: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my %stats = (idle=>0, ready=>0, running=>0, runable=>0, | |||||||||||||||||||||||||||||||||||||||||
| 85 | done=>0, parerr=>0, reapable=>0); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | map {$stats{$_->state}++} $self->processes; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return %stats; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | #### METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | sub schedule { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 414 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schedule: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 414 | return Parallel::Forker::Process->_new(_forkref=>$class, | |||||||||||||||||||||||||||||||||||||||||
| 95 | @_); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | sub sig_child { | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # Keep minimal to avoid coredumps | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 165 | ▶ | return if !$_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sig_child: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 165 | $_[0]->{_activity} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | sub wait_all { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 52 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wait_all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 52 | ▶ | while ($self->is_any_left) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | #print "NRUNNING ", scalar ( (keys %{$self->{_running}}) ), "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 749 | $self->poll; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 721 | usleep 100*1000; | |||||||||||||||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | sub reap_processes { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reap_processes: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my @reaped; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | foreach my $process ($self->processes) { | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | next unless $process->is_reapable; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $process->reap; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | push @reaped, $process; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | return @reaped; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | sub is_any_left { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 773 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_any_left: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 773 | ▶ | return 1 if ( (keys %{$self->{_runable}}) > 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 631 | ▶ | return 1 if ( (keys %{$self->{_running}}) > 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | sub find_proc_name { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 426 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_proc_name: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 426 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 134 | # Returns list of processes matching the name or label | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 426 | ▶ | if (exists $self->{_processes}{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 330 | return ($self->{_processes}{$name}); | |||||||||||||||||||||||||||||||||||||||||
| 137 | } elsif (exists $self->{_labels}{$name}) { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 64 | return @{$self->{_labels}{$name}}; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 32 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 34 | our $_Warned_Use_Sig_Child; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | sub poll { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 749 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
poll: called CC=12 SCAR=26.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 749 | ▶ | return if $self->use_sig_child && !$self->{_activity}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->use_sig_child and not $self->{"_activity"}
MC/DC100%2/2
$self->use_sig_child and not $self->{"_activity"}
$self->use_sig_child
not $self->{"_activity"}
Truth table100%3/3
A and not(B)
A= $self->use_sig_childB= not $self->{"_activity"}
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 226 | ▶ | if (!defined $self->use_sig_child) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | carp "%Warning: Forker object should be new'ed with use_sig_child=>0 or 1, " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$^W and not $Parallel::Forker::_Warned_Use_Sig_Child
MC/DC0%0/2
$^W and not $Parallel::Forker::_Warned_Use_Sig_Child
$^W
not $Parallel::Forker::_Warned_Use_Sig_Child
Truth table0%0/3
A and not(B)
A= $^WB= not $Parallel::Forker::_Warned_Use_Sig_Child
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | if ($^W && !$_Warned_Use_Sig_Child); | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $_Warned_Use_Sig_Child = 1; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | $self->use_sig_child(0); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # We don't have a loop around this any more, as we want to allow | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # applications to do other work. We'd also need to be careful not to | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # set _activity with no one runnable, as it would potentially cause a | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # infinite loop. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 226 | $self->{_activity} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 226 | my $nrunning = grep { not $_->poll } (values %{$self->{_running}}); | |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 226 | ▶ | if (!($self->{max_proc} && $nrunning >= $self->{max_proc})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
MC/DC0%0/2
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
$self->{"max_proc"}
$nrunning >= $self->{"max_proc"}
Truth table66%2/3
A and B
A= $self->{"max_proc"}B= $nrunning >= $self->{"max_proc"}
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 226 | foreach my $procref (sort {$a->{name} cmp $b->{name}} # Lanch in named order | |||||||||||||||||||||||||||||||||||||||||
| 165 | 226 | values %{$self->{_runable}}) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 243 | ▶ | last if ($self->{max_proc} && $nrunning >= $self->{max_proc}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
MC/DC100%2/2
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
$self->{"max_proc"}
$nrunning >= $self->{"max_proc"}
Truth table100%3/3
A and B
A= $self->{"max_proc"}B= $nrunning >= $self->{"max_proc"}
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 223 | $procref->run; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 195 | $nrunning++; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # If no one's running, we need _activity set to check for runable -> running | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # transitions during the next call to poll(). | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 198 | ▶ | $self->{_activity} = 1 if !$nrunning; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | sub ready_all { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 52 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ready_all: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 52 | foreach my $procref ($self->processes) { | |||||||||||||||||||||||||||||||||||||||||
| 179 | 414 | ▶ | $procref->ready if $procref->is_idle; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | sub kill_all { | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kill_all: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my $signal = shift || 9; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 9
MC/DC0%0/1
shift() || 9
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | foreach my $procref ($self->running_sorted) { | |||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | $procref->kill($signal); | |||||||||||||||||||||||||||||||||||||||||
| 188 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | sub kill_tree_all { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kill_tree_all: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $signal = shift || 9; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 9
MC/DC0%0/1
shift() || 9
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | foreach my $procref ($self->running_sorted) { | |||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | $procref->kill_tree($signal); | |||||||||||||||||||||||||||||||||||||||||
| 196 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | sub write_tree { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_tree: not called CC=28 SCAR=67.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | my %params = (@_); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | defined $params{filename} or croak "%Error: filename not specified,"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my %did_print; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my $another_loop = 1; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my $level = 0; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my $line = 4; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | while ($another_loop) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $another_loop = 0; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | $level++; | |||||||||||||||||||||||||||||||||||||||||
| 212 | proc: | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | foreach my $procref ($self->processes_sorted) { | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | foreach my $ra (values %{$procref->{_after_parents}}) { | |||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | next proc if (($did_print{$ra->{name}}{level}||999) >= $level); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$did_print{$ra->{'name'}}{'level'} || 999
MC/DC0%0/1
$did_print{$ra->{'name'}}{'level'} || 999
$did_print{$ra->{'name'}}{'level'}
Truth table0%0/2
A
A= $did_print{$ra->{'name'}}{'level'}
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if (!$did_print{$procref->{name}}{level}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $did_print{$procref->{name}}{level} = $level; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $did_print{$procref->{name}}{line} = $line; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $another_loop = 1; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $lines[$line][0] = $procref->_write_tree_line($level,0); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $lines[$line+1][0] = $procref->_write_tree_line($level,1); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | foreach my $ra (values %{$procref->{_after_parents}}) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | $lines[$line][$did_print{$ra->{name}}{line}] | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | = $procref->{_after_parents_op}{$ra->{name}}; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $line+=2; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | if ($Debug) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $lines[$line++][0] = $procref->_write_tree_line($level,2); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $lines[$line++][0] = $procref->_write_tree_line($level,3); | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | $lines[$line++][0] = $procref->_write_tree_line($level,4); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | $line++; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | $line++; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | if (0) { | |||||||||||||||||||||||||||||||||||||||||
| 240 | for (my $row=1; $row<$line; $row++) { | ||||||||||||||||||||||||||||||||||||||||||
| 241 | for (my $col=1; $col<$line; $col++) { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | print ($lines[$row][$col]?1:0); | ||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | print "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | for (my $col=1; $col<=$#lines; $col++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $col_used_row_min; | |||||||||||||||||||||||||||||||||||||||||
| 250 | my $col_used_row_max; | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | for (my $row=1; $row<=$#lines; $row++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if ($lines[$row][$col]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $col_used_row_min = min($col_used_row_min, $row); | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $col_used_row_max = max($col_used_row_max, $row); | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ($col_used_row_min) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $col_used_row_min = min($col_used_row_min, $col); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $col_used_row_max = max($col_used_row_max, $col); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | for (my $row=$col_used_row_min; $row<=$col_used_row_max; $row++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $lines[$row][$col] ||= '<' if $row==$col; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$lines[$row][$col] ||= "<"
MC/DC0%0/1
$lines[$row][$col] ||= "<"
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | $lines[$row][$col] ||= '|'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$lines[$row][$col] ||= "|"
MC/DC0%0/1
$lines[$row][$col] ||= "|"
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | for (my $row=1; $row<=$#lines; $row++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if (($lines[$row][0]||" ") !~ /^ /) { # Line with text on it | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$lines[$row][0] || ' '
MC/DC0%0/1
$lines[$row][0] || ' '
$lines[$row][0]
Truth table0%0/2
A
A= $lines[$row][0]
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $lines[$row][$col] ||= '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$lines[$row][$col] ||= "-"
MC/DC0%0/1
$lines[$row][$col] ||= "-"
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | #$lines[$row][$col-1] ||= '-'; | ||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | $lines[$row][$col] ||= ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$lines[$row][$col] ||= " "
MC/DC0%0/1
$lines[$row][$col] ||= " "
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | #$lines[$row][$col-1] ||= ' '; | ||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | my $fh = IO::File->new($params{filename},"w") or die "%Error: $! $params{filename},"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | print $fh "Tree of process spawn requirements:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | print $fh " & Indicates the program it connects to must complete with ok status\n"; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | print $fh " before the command on this row is allowed to become RUNABLE\n"; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | print $fh " E As with &, but with error status\n"; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | print $fh " ^ As with &, but with error or ok status\n"; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | print $fh " O Ored condition, either completing starts proc\n"; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | print $fh "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | for (my $row=1; $row<=$#lines; $row++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $line = ""; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | for (my $col=1; $col<$#lines; $col++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | $line .= ($lines[$row][$col]||""); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$lines[$row][$col] || ''
MC/DC0%0/1
$lines[$row][$col] || ''
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | $line .= $lines[$row][0]||""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$lines[$row][0] || ''
MC/DC0%0/1
$lines[$row][0] || ''
$lines[$row][0]
Truth table0%0/2
A
A= $lines[$row][0]
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | $line =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | print $fh "$line\n"; #if $line !~ /^\s*$/; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $fh->close; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | sub min { | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | my $rtn = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
min: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | foreach my $v (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | $rtn = $v if !defined $rtn || (defined $v && $v < $rtn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $rtn or defined $v and $v < $rtn
defined $v and $v < $rtn
MC/DC0%0/3
not defined $rtn or defined $v and $v < $rtn
not defined $rtn
defined $v
$v < $rtn
Truth table0%0/4
not(A) or B and C
A= not defined $rtnB= defined $vC= $v < $rtn
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | return $rtn; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | sub max { | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | my $rtn = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
max: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | foreach my $v (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | $rtn = $v if !defined $rtn || (defined $v && $v > $rtn); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $rtn or defined $v and $v > $rtn
defined $v and $v > $rtn
MC/DC0%0/3
not defined $rtn or defined $v and $v > $rtn
not defined $rtn
defined $v
$v > $rtn
Truth table0%0/4
not(A) or B and C
A= not defined $rtnB= defined $vC= $v > $rtn
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | return $rtn; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 313 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 314 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | Parallel::Forker - Parallel job forking and management | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | use Parallel::Forker; | ||||||||||||||||||||||||||||||||||||||||||
| 323 | $Fork = new Parallel::Forker (use_sig_child=>1); | ||||||||||||||||||||||||||||||||||||||||||
| 324 | $SIG{CHLD} = sub { Parallel::Forker::sig_child($Fork); }; | ||||||||||||||||||||||||||||||||||||||||||
| 325 | $SIG{TERM} = sub { $Fork->kill_tree_all('TERM') if $Fork && $Fork->in_parent; die "Quitting...\n"; }; | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | $Fork->schedule | ||||||||||||||||||||||||||||||||||||||||||
| 328 | (run_on_start => sub {print "child work here...";}, | ||||||||||||||||||||||||||||||||||||||||||
| 329 | # run_on_start => \&child_subroutine, # Alternative: call a named sub. | ||||||||||||||||||||||||||||||||||||||||||
| 330 | run_on_finish => sub {print "parent cleanup here...";}, | ||||||||||||||||||||||||||||||||||||||||||
| 331 | )->run; | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | $Fork->wait_all; # Wait for all children to finish | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # More processes | ||||||||||||||||||||||||||||||||||||||||||
| 336 | my $p1 = $Fork->schedule(...)->ready; | ||||||||||||||||||||||||||||||||||||||||||
| 337 | my $p2 = $Fork->schedule(..., run_after=>[$p1])->ready; | ||||||||||||||||||||||||||||||||||||||||||
| 338 | $Fork->wait_all; # p1 will complete before p2 starts | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | # Other functions | ||||||||||||||||||||||||||||||||||||||||||
| 341 | $Fork->poll; # Service any active children | ||||||||||||||||||||||||||||||||||||||||||
| 342 | foreach my $proc ($Fork->running) { # Loop on each running child | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | while ($Fork->is_any_left) { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | $Fork->poll; | ||||||||||||||||||||||||||||||||||||||||||
| 346 | usleep(10*1000); | ||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | Parallel::Forker manages parallel processes that are either subroutines or | ||||||||||||||||||||||||||||||||||||||||||
| 352 | system commands. Forker supports most of the features in all the other | ||||||||||||||||||||||||||||||||||||||||||
| 353 | little packages out there, with the addition of being able to specify | ||||||||||||||||||||||||||||||||||||||||||
| 354 | complicated expressions to determine which processes run after others, or | ||||||||||||||||||||||||||||||||||||||||||
| 355 | run when others fail. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | Function names are loosely based on Parallel::ForkManager. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | The unique property of Parallel::Forker is the ability to schedule | ||||||||||||||||||||||||||||||||||||||||||
| 360 | processes based on expressions that are specified when the processes are | ||||||||||||||||||||||||||||||||||||||||||
| 361 | defined. For example: | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | my $p1 = $Fork->schedule(..., label=>'p1'); | ||||||||||||||||||||||||||||||||||||||||||
| 364 | my $p2 = $Fork->schedule(..., label=>'p2'); | ||||||||||||||||||||||||||||||||||||||||||
| 365 | my $p3 = $Fork->schedule(..., run_after => ["p1 | p2"]); | ||||||||||||||||||||||||||||||||||||||||||
| 366 | my $p4 = $Fork->schedule(..., run_after => ["p1 & !p2"]); | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | Process p3 is specified to run after process p1 *or* p2 have completed | ||||||||||||||||||||||||||||||||||||||||||
| 369 | successfully. Process p4 will run after p1 finishes successfully, and | ||||||||||||||||||||||||||||||||||||||||||
| 370 | process p2 has completed with bad exit status. | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | For more examples, see the tests. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =item $self->find_proc_name(<name>) | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | Returns one or more Parallel::Forker::Process objects for the given name (one | ||||||||||||||||||||||||||||||||||||||||||
| 381 | object returned) or label (one or more objects returned). Returns undef if no | ||||||||||||||||||||||||||||||||||||||||||
| 382 | processes are found. | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =item $self->in_parent | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | Return true if and only if called from the parent process (the one that | ||||||||||||||||||||||||||||||||||||||||||
| 387 | created the Forker object). | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =item $self->is_any_left | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | Return true if any processes are running, or runnable (need to run). | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | =item $self->kill_all(<signal>) | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | Send a signal to all running children. You probably want to call this only | ||||||||||||||||||||||||||||||||||||||||||
| 396 | from the parent process that created the Parallel::Forker object, wrap the | ||||||||||||||||||||||||||||||||||||||||||
| 397 | call in "if ($self->in_parent)." | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =item $self->kill_tree_all(<signal>) | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | Send a signal to all running children and their subchildren. | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | =item $self->max_proc(<number>) | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | Specify the maximum number of processes that the poll method will run at | ||||||||||||||||||||||||||||||||||||||||||
| 406 | any one time. Defaults to undef, which runs all possible jobs at once. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | Max_proc takes effect when you schedule processes and mark them "ready," | ||||||||||||||||||||||||||||||||||||||||||
| 408 | then rely on Parallel::Forker's poll method to move the processes from the | ||||||||||||||||||||||||||||||||||||||||||
| 409 | ready state to the run state. (You should not call ->run yourself, as this | ||||||||||||||||||||||||||||||||||||||||||
| 410 | starts a new process immediately, ignoring max_proc.) | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | =item $self->new(<parameters>) | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | Create a new manager object. There may be more than one manager in any | ||||||||||||||||||||||||||||||||||||||||||
| 415 | application, but applications taking advantage of the sig_child handler | ||||||||||||||||||||||||||||||||||||||||||
| 416 | should call every manager's C<sig_child> method in the application's | ||||||||||||||||||||||||||||||||||||||||||
| 417 | C<SIGCHLD> handler. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | Parameters are passed by name as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | =item max_proc => (<number>) | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | See the C<max_proc> object method. | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =item use_sig_child => ( 0 | 1 ) | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | See the C<use_sig_child> object method. This option must be specified to | ||||||||||||||||||||||||||||||||||||||||||
| 430 | prevent a warning. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | =item $self->poll | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | See if any children need work, and service them. Start up to max_proc | ||||||||||||||||||||||||||||||||||||||||||
| 437 | processes that are "ready" by calling their run method. Non-blocking; | ||||||||||||||||||||||||||||||||||||||||||
| 438 | always returns immediately. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =item $self->process(<process_name>) | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | Return Parallel::Forker::Process object for the specified process name, or | ||||||||||||||||||||||||||||||||||||||||||
| 443 | undef if none is found. See also find_proc_name. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | =item $self->processes | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | Return Parallel::Forker::Process objects for all processes. | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =item $self->processes_sorted | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | Return Parallel::Forker::Process objects for all processes, sorted by name. | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =item $self->ready_all | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | Mark all processes as ready for scheduling. | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | =item $self->reap_processes | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | Reap all processes which have no other processes waiting for them, and the | ||||||||||||||||||||||||||||||||||||||||||
| 460 | process is is_done or is_parerr. Returns list of processes reaped. This | ||||||||||||||||||||||||||||||||||||||||||
| 461 | reclaims memory for when a large number of processes are being created, | ||||||||||||||||||||||||||||||||||||||||||
| 462 | run, and destroyed. | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | =item $self->running | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | Return Parallel::Forker::Process objects for all processes that are | ||||||||||||||||||||||||||||||||||||||||||
| 467 | currently running. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | =item $self->schedule(<parameters>) | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | Register a new process perhaps for later running. Returns a | ||||||||||||||||||||||||||||||||||||||||||
| 472 | Parallel::Forker::Process object. Parameters are passed by name as | ||||||||||||||||||||||||||||||||||||||||||
| 473 | follows: | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =item label | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | Optional name to use in C<run_after> commands. Unlike C<name>, this may be | ||||||||||||||||||||||||||||||||||||||||||
| 480 | reused, in which case C<run_after> will wait on all commands with the given | ||||||||||||||||||||||||||||||||||||||||||
| 481 | label. Labels must contain only [a-zA-Z0-9_]. | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | =item name | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | Optional name to use in C<run_after> commands. Note that names MUST be | ||||||||||||||||||||||||||||||||||||||||||
| 486 | unique! When not specified, a unique number will be assigned | ||||||||||||||||||||||||||||||||||||||||||
| 487 | automatically. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =item run_on_start | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | Subroutine reference to execute when the job begins, in the forked process. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | The subroutine is called with one argument, a reference to the | ||||||||||||||||||||||||||||||||||||||||||
| 493 | Parallel::Forker::Process that is starting. | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | If your callback is going to fork, you'd be advised to have the child: | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | $SIG{ALRM} = 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||||
| 498 | $SIG{CHLD} = 'DEFAULT'; | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | This will prevent the child from inheriting the parent's handlers, and | ||||||||||||||||||||||||||||||||||||||||||
| 501 | possibly confusing any child calls to waitpid. | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | =item run_on_finish | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | Subroutine reference to execute when the job ends, in the master process. | ||||||||||||||||||||||||||||||||||||||||||
| 506 | The subroutine is called with two arguments, a reference to the | ||||||||||||||||||||||||||||||||||||||||||
| 507 | Parallel::Forker::Process that is finishing, and the exit status of the | ||||||||||||||||||||||||||||||||||||||||||
| 508 | child process. Note the exit status will only be correct if a CHLD signal | ||||||||||||||||||||||||||||||||||||||||||
| 509 | handler is installed. | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | =item run_pre_start | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | Subroutine reference to execute before forking the child, in the master | ||||||||||||||||||||||||||||||||||||||||||
| 514 | process. The subroutine is called with one argument, a reference to the | ||||||||||||||||||||||||||||||||||||||||||
| 515 | Parallel::Forker::Process that is starting. | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | =item run_after | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | A list reference of processes that must be completed before this process | ||||||||||||||||||||||||||||||||||||||||||
| 520 | can be runnable. You may pass a process object (from schedule), a process | ||||||||||||||||||||||||||||||||||||||||||
| 521 | name, or a process label. You may use "|" or "&" in a string to run this | ||||||||||||||||||||||||||||||||||||||||||
| 522 | process after ANY processes exit, or after ALL exit (the default.) | ||||||||||||||||||||||||||||||||||||||||||
| 523 | ! in front of a process name indicates to run if that process fails with | ||||||||||||||||||||||||||||||||||||||||||
| 524 | bad exit status. ^ in front of a process indicates to run if that process | ||||||||||||||||||||||||||||||||||||||||||
| 525 | succeeds OR fails. | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =item $self->sig_child | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | Must be called in a C<$SIG{CHLD}> handler by the parent process if | ||||||||||||||||||||||||||||||||||||||||||
| 532 | C<use_sig_child> was called with a "true" value. If there are multiple | ||||||||||||||||||||||||||||||||||||||||||
| 533 | Parallel::Forker objects each of their C<sig_child> methods must be called | ||||||||||||||||||||||||||||||||||||||||||
| 534 | in the C<$SIG{CHLD}> handler. | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | =item $self->state_stats | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | Return hash containing statistics with keys of state names, and values with | ||||||||||||||||||||||||||||||||||||||||||
| 539 | number of processes in each state. | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | =item $self->use_sig_child( 0 | 1 ) | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | This should always be called with a 0 or 1. If you install a C<$SIG{CHLD}> | ||||||||||||||||||||||||||||||||||||||||||
| 544 | handler which calls your Parallel::Forker object's C<sig_child> method, you | ||||||||||||||||||||||||||||||||||||||||||
| 545 | should also turn on C<use_sig_child>, by calling it with a "true" argument. | ||||||||||||||||||||||||||||||||||||||||||
| 546 | Then, calls to C<poll()> will do less work when there are no children | ||||||||||||||||||||||||||||||||||||||||||
| 547 | processes to be reaped. If not using the handler call with 0 to prevent a | ||||||||||||||||||||||||||||||||||||||||||
| 548 | warning. | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | =item $self->wait_all | ||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | Wait until there are no running or runable jobs left. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | =item $self->write_tree(filename => <filename>) | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | Print a dump of the execution tree. | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | =head1 DISTRIBUTION | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | The latest version is available from CPAN and from | ||||||||||||||||||||||||||||||||||||||||||
| 563 | L<https://www.veripool.org/parallel-forker>. | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | Copyright 2002-2020 by Wilson Snyder. This package is free software; you | ||||||||||||||||||||||||||||||||||||||||||
| 566 | can redistribute it and/or modify it under the terms of either the GNU | ||||||||||||||||||||||||||||||||||||||||||
| 567 | Lesser General Public License Version 3 or the Perl Artistic License | ||||||||||||||||||||||||||||||||||||||||||
| 568 | Version 2.0. | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | Wilson Snyder <wsnyder@wsnyder.org> | ||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | L<Parallel::Forker::Process> | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 579 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||