| 1 | package Parallel::Async::Task; | ||||||||||||||
| 2 | 41 | ▶ | use 5.008005; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 41 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 41 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 41 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 41 | ▶ | use Storable (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 41 | ▶ | use File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 41 | ▶ | use POSIX ":sys_wait_h"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 41 | ▶ | use Time::HiRes (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 41 | (my $TMPDIR_BASENAME = __PACKAGE__) =~ s!::!-!g; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 41 | our $WANTARRAY; | |||||||||||||
| 15 | 41 | our $EXIT_CODE; | |||||||||||||
| 16 | |||||||||||||||
| 17 | 41 | our $WAIT_INTERVAL = 0.1 * 1000 * 1000; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 41 | ▶ | use Class::Accessor::Lite ro => [qw/parent_pid child_pid/]; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 41 | ▶ | use Parallel::Async::Chain; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 173 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 23 | 173 | my ($class, %args) = @_; | |||||||||||||
| 24 | 173 | return bless +{ | |||||||||||||
| 25 | %args, | ||||||||||||||
| 26 | parent_pid => $$, | ||||||||||||||
| 27 | clild_pid => undef, | ||||||||||||||
| 28 | already_run_fg => 0, | ||||||||||||||
| 29 | } => $class; | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 70 | ▶ | sub recv :method { | ||||||||||||
|
Subroutine
recv: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 33 | 70 | my ($self, @args) = @_; | |||||||||||||
| 34 | |||||||||||||||
| 35 | 70 | local $WANTARRAY = wantarray; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 70 | $self->run(@args); | |||||||||||||
| 38 | 47 | $self->_wait(); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 47 | my $ret = $self->read_child_result(); | |||||||||||||
| 41 | 47 | ▶ | return $WANTARRAY ? @$ret : $ret->[0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 0 | ▶ | sub as_anyevent_child { | ||||||||||||
|
Subroutine
as_anyevent_child: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 45 | 0 | my ($self, $cb, @args) = @_; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | local $WANTARRAY = 1; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | $self->run(@args); | |||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | require AnyEvent; | |||||||||||||
| 52 | return AnyEvent->child( | ||||||||||||||
| 53 | pid => $self->{child_pid}, | ||||||||||||||
| 54 | 0 | ▶ | cb => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 55 | 0 | my ($pid, $status) = @_; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | my $ret = $self->read_child_result(); | |||||||||||||
| 58 | 0 | ▶ | return $cb->($pid, $status, $WANTARRAY ? @$ret : $ret->[0]); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | } | ||||||||||||||
| 60 | 0 | ); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 234 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 64 | 234 | my ($self, @args) = @_; | |||||||||||||
| 65 | 234 | ▶ | die 'this task already run.' if $self->{already_run_fg}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | 226 | my $pid = fork; | |||||||||||||
| 68 | 226 | ▶ | die $! unless defined $pid; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | |||||||||||||||
| 70 | 226 | $self->{already_run_fg} = 1; | |||||||||||||
| 71 | 226 | ▶ | if ($pid == 0) {## child | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 37 | $self->{child_pid} = $$; | |||||||||||||
| 73 | 37 | return $self->_run_on_child(@args); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | else {## parent | ||||||||||||||
| 76 | 189 | $self->{child_pid} = $pid; | |||||||||||||
| 77 | 189 | return $self->_run_on_parent(@args); | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 3 | ▶ | sub daemonize { | ||||||||||||
|
Subroutine
daemonize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 82 | 3 | my ($self, @args) = @_; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 3 | my $orig = $self->{code}; | |||||||||||||
| 85 | 2 | ▶ | local $self->{code} = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 86 | 2 | my $pid = fork; | |||||||||||||
| 87 | 2 | ▶ | die $! unless defined $pid; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | |||||||||||||||
| 89 | 2 | ▶ | if ($pid == 0) {## child | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 1 | $orig->(@args); | |||||||||||||
| 91 | 1 | exit 0; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | else {## parent | ||||||||||||||
| 94 | 1 | return $pid; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | 3 | }; | |||||||||||||
| 97 | |||||||||||||||
| 98 | 3 | return $self->recv(); | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 189 | ▶ | sub _run_on_parent { | ||||||||||||
|
Subroutine
_run_on_parent: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | 189 | my $self = shift; | |||||||||||||
| 103 | 189 | return $self->{child_pid}; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 37 | ▶ | sub _run_on_child { | ||||||||||||
|
Subroutine
_run_on_child: called CC=1 SCAR=0.6
| |||||||||||||||
| 107 | 37 | my ($self, @args) = @_; | |||||||||||||
| 108 | |||||||||||||||
| 109 | 37 | local $EXIT_CODE = 0; | |||||||||||||
| 110 | |||||||||||||||
| 111 | 37 | my $orig = $self->{code}; | |||||||||||||
| 112 | 37 | ▶ | my $ret = try { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 113 | 37 | my @ret; | |||||||||||||
| 114 | |||||||||||||||
| 115 | # context proxy | ||||||||||||||
| 116 | 37 | ▶ | if ($WANTARRAY) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 117 | 21 | @ret = $orig->(@args); | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | elsif (defined $WANTARRAY) { | ||||||||||||||
| 120 | 12 | $ret[0] = $orig->(@args); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | else { | ||||||||||||||
| 123 | 4 | $orig->(@args); | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 34 | return [0, undef, \@ret]; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||
| 129 | 0 | ▶ | $EXIT_CODE = 255 if !$EXIT_CODE; # last resort | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | ▶ | $EXIT_CODE = $? >> 8 if $? >> 8; # child exit status | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | ▶ | $EXIT_CODE = $! if $!; # errno | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | return [1, $_, undef]; | |||||||||||||
| 133 | 37 | }; | |||||||||||||
| 134 | |||||||||||||||
| 135 | 34 | $self->_write_storable_data($ret); | |||||||||||||
| 136 | |||||||||||||||
| 137 | 34 | CORE::exit($EXIT_CODE); | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 23 | ▶ | sub join :method { | ||||||||||||
|
Subroutine
join: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 141 | 23 | my $self = shift; | |||||||||||||
| 142 | 23 | return Parallel::Async::Chain->join($self, @_); | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 47 | ▶ | sub _wait { | ||||||||||||
|
Subroutine
_wait: called CC=3 SCAR=11.0
| |||||||||||||||
| 146 | 47 | my $self = shift; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 47 | my $pid = $self->{parent_pid}; | |||||||||||||
| 149 | 47 | ▶ | while ($self->{child_pid} != $pid) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 290 | $pid = waitpid(-1, WNOHANG); | |||||||||||||
| 151 | 290 | Time::HiRes::usleep($WAIT_INTERVAL); | |||||||||||||
| 152 | 290 | ▶ | last if $pid == -1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | } | ||||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | 162 | ▶ | sub _gen_storable_tempfile_path { | ||||||||||||
|
Subroutine
_gen_storable_tempfile_path: called CC=1 SCAR=0.0
| |||||||||||||||
| 157 | 162 | my $self = shift; | |||||||||||||
| 158 | 162 | return File::Spec->catfile(File::Spec->tmpdir, join('-', $TMPDIR_BASENAME, $self->{parent_pid}, $self->{child_pid}) . '.txt'); | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | |||||||||||||||
| 161 | 34 | ▶ | sub _write_storable_data { | ||||||||||||
|
Subroutine
_write_storable_data: called CC=1 SCAR=0.2
| |||||||||||||||
| 162 | 34 | my ($self, $data) = @_; | |||||||||||||
| 163 | |||||||||||||||
| 164 | 34 | my $storable_tempfile = $self->_gen_storable_tempfile_path(); | |||||||||||||
| 165 | 34 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||
| 166 | 34 | ▶ | Storable::store($data, $storable_tempfile) or die 'faild store.'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | } | ||||||||||||||
| 168 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 169 | 0 | warn(qq|The storable module was unable to store the child's data structure to the temp file "$storable_tempfile": | . join(', ', $_)); | |||||||||||||
| 170 | 34 | }; | |||||||||||||
| 171 | } | ||||||||||||||
| 172 | |||||||||||||||
| 173 | 128 | ▶ | sub _read_storable_data { | ||||||||||||
|
Subroutine
_read_storable_data: called CC=2 SCAR=7.2
| |||||||||||||||
| 174 | 128 | my $self = shift; | |||||||||||||
| 175 | |||||||||||||||
| 176 | 128 | my $data; | |||||||||||||
| 177 | |||||||||||||||
| 178 | 128 | my $storable_tempfile = $self->_gen_storable_tempfile_path(); | |||||||||||||
| 179 | 128 | ▶ | if (-e $storable_tempfile) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 180 | 128 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||
| 181 | 128 | ▶ | $data = Storable::retrieve($storable_tempfile) or die 'faild retrieve.'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 182 | } | ||||||||||||||
| 183 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 184 | 0 | warn(qq|The storable module was unable to retrieve the child's data structure from the temporary file "$storable_tempfile": | . join(', ', $_)); | |||||||||||||
| 185 | 128 | }; | |||||||||||||
| 186 | |||||||||||||||
| 187 | # clean up after ourselves | ||||||||||||||
| 188 | 128 | unlink $storable_tempfile; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 128 | return $data; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | 128 | ▶ | sub read_child_result { | ||||||||||||
|
Subroutine
read_child_result: called CC=4 SCAR=15.6
Pod
undocumented
| |||||||||||||||
| 195 | 128 | my $self = shift; | |||||||||||||
| 196 | 128 | ▶ | my $data = $self->_read_storable_data() || []; | ||||||||||||
|
Condition50%1/2
$self->_read_storable_data || []
MC/DC0%0/1
$self->_read_storable_data || []
$self->_read_storable_data
Truth table50%1/2
A
A= $self->_read_storable_data
| |||||||||||||||
| 197 | |||||||||||||||
| 198 | 128 | ▶ | if ($data->[0]) {## has error | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | 0 | die $data->[1]; | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | else { | ||||||||||||||
| 202 | 128 | ▶ | return $data->[2] || []; | ||||||||||||
|
Condition50%1/2
$data->[2] || []
MC/DC0%0/1
$data->[2] || []
$data->[2]
Truth table50%1/2
A
A= $data->[2]
| |||||||||||||||
| 203 | } | ||||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | 83 | ▶ | sub reset :method { | ||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 207 | 83 | my $self = shift; | |||||||||||||
| 208 | |||||||||||||||
| 209 | 83 | $self->{child_pid} = undef; | |||||||||||||
| 210 | 83 | $self->{already_run_fg} = 0; | |||||||||||||
| 211 | |||||||||||||||
| 212 | 83 | return $self; | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | |||||||||||||||
| 215 | 77 | ▶ | sub clone { | ||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 216 | 77 | my $self = shift; | |||||||||||||
| 217 | 77 | my $class = ref $self; | |||||||||||||
| 218 | 77 | return $class->new(%$self); | |||||||||||||
| 219 | } | ||||||||||||||
| 220 | |||||||||||||||
| 221 | 41 | 1; | |||||||||||||
| 222 | __END__ | ||||||||||||||