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