| 1 | package IPC::QWorker; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 12 | ▶ | use 5.000; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 12 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | # ABSTRACT: processing a queue in parallel | ||||||||||||||||||||
| 9 | 12 | our $VERSION = '0.08'; # VERSION | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 12 | our $DEBUG = 0; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 12 | ▶ | use IO::Select; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 12 | ▶ | use IPC::QWorker::Worker; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 11 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 19 | 11 | my $this = shift; | |||||||||||||||||||
| 20 | 11 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 21 | 11 | my $self = { | |||||||||||||||||||
| 22 | '_workers' => [], | ||||||||||||||||||||
| 23 | '_queue' => [], | ||||||||||||||||||||
| 24 | '_pids' => {}, | ||||||||||||||||||||
| 25 | '_ready_workers' => [], | ||||||||||||||||||||
| 26 | '_io_select' => IO::Select->new(), | ||||||||||||||||||||
| 27 | @_ | ||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 11 | bless( $self, $class ); | |||||||||||||||||||
| 31 | 11 | return ($self); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 11 | ▶ | sub create_workers { | ||||||||||||||||||
|
Subroutine
create_workers: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | 11 | my $self = shift(); | |||||||||||||||||||
| 36 | 11 | my $num_workers = shift(); | |||||||||||||||||||
| 37 | 11 | my $worker; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 11 | ▶ | for ( my $i = 0 ; $i < $num_workers ; $i++ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | # create the worker | ||||||||||||||||||||
| 41 | 65 | $worker = IPC::QWorker::Worker->new(@_); | |||||||||||||||||||
| 42 | # add him to the list of workers | ||||||||||||||||||||
| 43 | 55 | push( @{ $self->{'_workers'} }, $worker); | |||||||||||||||||||
| 44 | # add him to the pid->workers index | ||||||||||||||||||||
| 45 | 55 | $self->{'_pids'}->{$worker->{'pid'}} = $worker; | |||||||||||||||||||
| 46 | # add him to IO::Select | ||||||||||||||||||||
| 47 | 55 | $self->{'_io_select'}->add( $worker->{'pipe'} ); | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 121 | ▶ | sub push_queue { | ||||||||||||||||||
|
Subroutine
push_queue: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 52 | 121 | my $self = shift; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 121 | push( @{ $self->{'_queue'} }, @_ ); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 40 | ▶ | sub _get_ready_workers { | ||||||||||||||||||
|
Subroutine
_get_ready_workers: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 58 | 40 | my $self = shift(); | |||||||||||||||||||
| 59 | 40 | my $timeout = shift(); | |||||||||||||||||||
| 60 | 40 | my @can_read_pipes; | |||||||||||||||||||
| 61 | my $i; | ||||||||||||||||||||
| 62 | 40 | my $wpid; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # if we have no ready workers find some | ||||||||||||||||||||
| 65 | 40 | @can_read_pipes = $self->{'_io_select'}->can_read($timeout); | |||||||||||||||||||
| 66 | 40 | ▶ | if ($IPC::QWorker::DEBUG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | print STDERR "found " . scalar(@can_read_pipes) . " ready workers!\n"; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 40 | foreach $i (@can_read_pipes) { | |||||||||||||||||||
| 70 | # get pid from a msg like "12345 READY\n" | ||||||||||||||||||||
| 71 | 130 | ($wpid) = split(' ', readline($i)); | |||||||||||||||||||
| 72 | 130 | $self->{'_pids'}->{$wpid}->{'ready'} = 1; | |||||||||||||||||||
| 73 | 130 | push(@{$self->{'_ready_workers'}}, $self->{'_pids'}->{$wpid}); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 2 | ▶ | sub process_queue { | ||||||||||||||||||
|
Subroutine
process_queue: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 78 | 2 | my $self = shift; | |||||||||||||||||||
| 79 | 2 | my $timeout = shift; | |||||||||||||||||||
| 80 | 2 | my $qentry; | |||||||||||||||||||
| 81 | my $worker; | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | ▶ | if(defined($timeout)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | # if timeout is set wait for timeout till a worker is ready | ||||||||||||||||||||
| 85 | 1 | $self->_get_ready_workers($timeout); | |||||||||||||||||||
| 86 | 1 | ▶ | while($worker = shift(@{$self->{'_ready_workers'}})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 10 | $worker->send_entry(shift(@{ $self->{'_queue'}})); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||
| 90 | # loop over the Q till its empty | ||||||||||||||||||||
| 91 | # will block while waiting for ready workers | ||||||||||||||||||||
| 92 | # returns when the queue is empty | ||||||||||||||||||||
| 93 | 1 | ▶ | while($qentry = shift(@{ $self->{'_queue'}})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 110 | ▶ | while(!scalar(@{$self->{'_ready_workers'}})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 35 | ▶ | if ($IPC::QWorker::DEBUG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | print STDERR "no ready workers. wait for workers...\n"; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 35 | $self->_get_ready_workers(); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 110 | $worker = shift(@{$self->{'_ready_workers'}}); | |||||||||||||||||||
| 102 | 110 | $worker->send_entry($qentry); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 5 | ▶ | sub _get_busy_workers { | ||||||||||||||||||
|
Subroutine
_get_busy_workers: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 108 | 5 | my $self = shift(); | |||||||||||||||||||
| 109 | 5 | my @result; | |||||||||||||||||||
| 110 | my $worker; | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 5 | foreach $worker (@{$self->{'_workers'}}) { | |||||||||||||||||||
| 113 | 50 | ▶ | if(!$worker->{'ready'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 13 | push(@result, $worker); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | 5 | return(@result); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # will block till all workers are finished | ||||||||||||||||||||
| 121 | 1 | ▶ | sub flush_queue { | ||||||||||||||||||
|
Subroutine
flush_queue: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||
| 122 | 1 | my $self = shift(); | |||||||||||||||||||
| 123 | 1 | my @busy_workers; | |||||||||||||||||||
| 124 | 1 | my $select = IO::Select->new(); | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 1 | ▶ | while(scalar(@busy_workers = $self->_get_busy_workers())) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 4 | ▶ | if ($IPC::QWorker::DEBUG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 0 | print STDERR "still " . scalar(@busy_workers) . " busy workers...\n"; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | 4 | $self->_get_ready_workers(); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1 | ▶ | sub stop_workers { | ||||||||||||||||||
|
Subroutine
stop_workers: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||
| 136 | 1 | my $worker; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | # may be we could also use signals here | ||||||||||||||||||||
| 139 | 1 | foreach $worker ( @{ $self->{'_workers'} } ) { | |||||||||||||||||||
| 140 | 10 | $worker->exit_child(); | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 12 | 1; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | # vim:ts=2:expandtab:syntax=perl: | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||