ProcessPool.pm

filter: Statementstmt 98.5% 69 / 70 Branchbran 71.4% 10 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 50.0% 2 / 4 totaltotal 90.9% 101 / 111 SCAR 26.4
CC
14
Cov
93%
DESTROY
8.1
new
3.0
join
3.0
CRAP 14.1
« ProcessPool/Process.pm ↑ Summary ProcessPool/Worker.pm »
1 package AnyEvent::ProcessPool;
2 # ABSTRACT: Asynchronously runs code concurrently in a pool of perl processes
3 2 $AnyEvent::ProcessPool::VERSION = '0.06';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use AnyEvent::ProcessPool::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnyEvent::ProcessPool::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use AnyEvent::ProcessPool::Util qw(next_id cpu_count);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 4 ▶ my ($class, %param) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
14  
15 my $self = bless {
16 workers => $param{workers} || cpu_count,
17 limit => $param{limit},
18 include => $param{include},
19 4 ▶ pool => [], # array of AE::PP::Process objects
Condition66%2/3
$param{'workers'} || cpu_count()
l!l&&r!l&&!r
130
MC/DC0%0/2
$param{'workers'} || cpu_count()
$param{'workers'} cpu_count()
Truth table66%2/3
A || B
A= $param{'workers'}B= cpu_count()
ABresult
1X1
011
000
20 queue => [], # array of [id, code] tasks
21 complete => {}, # task_id => condvar: signals result to caller
22 pending => {}, # task_id => condvar: signals result internally
23 }, $class;
24  
25 # Initialize workers but do not yet wait for them to be started
26 4 foreach (1 .. $self->{workers}) {
27 my $worker = AnyEvent::ProcessPool::Process->new(
28 limit => $self->{limit},
29 include => $self->{include},
30 41 );
31 41 push @{$self->{pool}}, $worker;
32 }
33  
34 4 return $self;
35 }
36  
37 sub join {
38 1 ▶ my $self = shift;
Subroutine join: called CC=3 SCAR=11.0
Pod documented
39 1 foreach my $task_id (keys %{$self->{complete}}) {
40 10 ▶ if (my $cv = $self->{complete}{$task_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cv = $self->{"complete"}{$task_id}) 2 8 10
41 2 $cv->recv;
42 }
43 }
44 }
45  
46 sub DESTROY {
47 4 ▶ my ($self, $global) = @_;
Subroutine DESTROY: called CC=7 SCAR=20.9
48  
49 4 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 4 0 4
50 # Unblock watchers for any remaining pending tasks
51 4 ▶ if (ref $self->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"pending"}) 4 0 4
52 4 foreach my $cv (values %{$self->{pending}}) {
53 0 $cv->croak('AnyEvent::ProcessPool destroyed with pending tasks remaining');
54 }
55 }
56  
57 # Terminate any workers still alive
58 4 ▶ if (ref $self->{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"pool"}) 4 0 4
59 4 foreach my $worker (@{$self->{pool}}) {
60 41 ▶ $worker->stop if $worker;
Branch: 1 branch
BranchTrueFalseTotal
if $worker 41 0 41
61 }
62 }
63 }
64 }
65  
66 sub async {
67 23 ▶ my ($self, $code, @args) = @_;
Subroutine async: called CC=1 SCAR=0.0
Pod documented
68 23 my $id = next_id;
69 23 my $task = AnyEvent::ProcessPool::Task->new($code, \@args);
70 23 $self->{complete}{$id} = AE::cv;
71 23 push @{$self->{queue}}, [$id, $task];
72 23 $self->process_queue;
73 23 return $self->{complete}{$id};
74 }
75  
76 sub process_queue {
77 46 ▶ my $self = shift;
Subroutine process_queue: called CC=3 SCAR=11.0
Pod undocumented
78 46 my $queue = $self->{queue};
79 46 my $pool = $self->{pool};
80  
81 46 ▶ while (@$queue && @$pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue and @$pool) 23 46 69
Condition100%3/3
@$queue and @$pool
!ll&&!rl&&r
291723
MC/DC100%2/2
@$queue and @$pool
@$queue @$pool
Truth table100%3/3
A and B
A= @$queueB= @$pool
ABresult
0X0
100
111
82 23 my ($id, $task) = @{shift @$queue};
83 23 my $worker = shift @$pool;
84  
85 23 $self->{pending}{$id} = $worker->run($task);
86  
87 $self->{pending}{$id}->cb(sub{
88 23 ▶ my $task = shift->recv;
Subroutine __ANON__: called CC=2 SCAR=6.9
89  
90 23 ▶ if ($task->failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task->failed) { } 1 22 23
91 1 $self->{complete}{$id}->croak($task->result);
92 } else {
93 22 $self->{complete}{$id}->send($task->result);
94 }
95  
96 23 delete $self->{pending}{$id};
97 23 delete $self->{complete}{$id};
98  
99 23 push @$pool, $worker;
100 23 $self->process_queue;
101 23 });
102 }
103 }
104  
105 2 1;
106  
107 __END__
« ProcessPool/Process.pm ↑ Summary ProcessPool/Worker.pm »