« lib/Mojo/IOLoop/ReadWriteProcess/CGroup/v1.pm
↑ Summary
lib/Mojo/IOLoop/ReadWriteProcess/Pool.pm »
| 1 | package Mojo::IOLoop::ReadWriteProcess::Queue; | ||||||||||||||||||||||||||||
| 2 | 39 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 39 | ▶ | use Mojo::IOLoop::ReadWriteProcess::Pool; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 39 | ▶ | use Mojo::IOLoop::ReadWriteProcess; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 39 | ▶ | use Mojo::IOLoop::ReadWriteProcess::Session; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 39 | ▶ | use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | has queue => sub { Mojo::IOLoop::ReadWriteProcess::Pool->new() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | has pool => sub { Mojo::IOLoop::ReadWriteProcess::Pool->new() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | has done => sub { Mojo::IOLoop::ReadWriteProcess::Pool->new() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | has session => sub { Mojo::IOLoop::ReadWriteProcess::Session->singleton }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 40 | ▶ | sub _dequeue { | ||||||||||||||||||||||||||
|
Subroutine
_dequeue: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 15 | 40 | my ($self, $process) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 40 | ▶ | $self->pool($self->pool->grep(sub { $process ne $_ })); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 40 | ▶ | shift @{$self->queue} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->queue->first and $self->pool->add($self->queue->first)
MC/DC50%1/2
$self->queue->first and $self->pool->add($self->queue->first)
$self->queue->first
$self->pool->add($self->queue->first)
Truth table66%2/3
A and B
A= $self->queue->firstB= $self->pool->add($self->queue->first)
| |||||||||||||||||||||||||||||
| 19 | if ($self->queue->first && $self->pool->add($self->queue->first)); | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 904 | ▶ | sub exhausted { $_[0]->pool->size == 0 && shift->queue->size == 0 } | ||||||||||||||||||||||||||
|
Subroutine
exhausted: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | sub consume { | ||||||||||||||||||||||||||
|
Subroutine
consume: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | 4 | $self->session->enable; | |||||||||||||||||||||||||||
| 27 | 4 | $self->done->maximum_processes( | |||||||||||||||||||||||||||
| 28 | $self->queue->maximum_processes + $self->pool->maximum_processes); | ||||||||||||||||||||||||||||
| 29 | 4 | ▶ | until ($self->exhausted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 900 | sleep .5; | |||||||||||||||||||||||||||
| 31 | 900 | $self->session->consume_collected_info; | |||||||||||||||||||||||||||
| 32 | $self->session->_protect( | ||||||||||||||||||||||||||||
| 33 | 900 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | $self->pool->each( | ||||||||||||||||||||||||||||
| 35 | 1038 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 36 | 1038 | my $p = shift; | |||||||||||||||||||||||||||
| 37 | 1038 | ▶ | return unless $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1038 | ▶ | return if exists $p->{started}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 40 | $p->{started}++; | |||||||||||||||||||||||||||
| 40 | 40 | ▶ | $p->once(stop => sub { $self->done->add($p); $self->_dequeue($p) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 40 | $p->start; | |||||||||||||||||||||||||||
| 42 | 900 | }); | |||||||||||||||||||||||||||
| 43 | 900 | }); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 40 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 40 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 40 | ▶ | $self->pool->add(@_) // $self->queue->add(@_); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->pool->add(@_) // $self->queue->add(@_)
MC/DC0%0/2
$self->pool->add(@_) // $self->queue->add(@_)
$self->pool->add(@_)
$self->queue->add(@_)
Truth table66%2/3
A // B
A= $self->pool->add(@_)B= $self->queue->add(@_)
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 53 | 4 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 54 | 4 | my $fn = $AUTOLOAD; | |||||||||||||||||||||||||||
| 55 | 4 | $fn =~ s/.*:://; | |||||||||||||||||||||||||||
| 56 | 4 | ▶ | return if $fn eq "DESTROY"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | return ( | ||||||||||||||||||||||||||||
| 59 | 3 | ▶ | eval { $self->pool->Mojo::IOLoop::ReadWriteProcess::Pool::_cmd(@_, $fn) }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | (grep(/once|on|emit/, $fn)) | ||||||||||||||||||||||||||||
| 61 | 3 | ? eval { $self->queue->Mojo::IOLoop::ReadWriteProcess::Pool::_cmd(@_, $fn) } | |||||||||||||||||||||||||||
| 62 | : ()); | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 39 | 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Mojo::IOLoop::ReadWriteProcess::Queue - Queue for Mojo::IOLoop::ReadWriteProcess objects. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | use Mojo::IOLoop::ReadWriteProcess qw(queue process); | ||||||||||||||||||||||||||||
| 76 | my $n_proc = 20; | ||||||||||||||||||||||||||||
| 77 | my $fired; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | my $q = queue; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | $q->pool->maximum_processes(2); # Max 2 processes in parallel | ||||||||||||||||||||||||||||
| 82 | $q->queue->maximum_processes(10); # Max queue is 10 | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | $q->add( process sub { return 42 } ) for 1..7; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Subscribe to all "stop" events in the pool | ||||||||||||||||||||||||||||
| 87 | $q->once(stop => sub { $fired++; }); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Consume the queue | ||||||||||||||||||||||||||||
| 90 | $q->consume(); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $all = $q->done; # All processes, Mojo::Collection of Mojo::IOLoop::ReadWriteProcess | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Set your own running pool | ||||||||||||||||||||||||||||
| 95 | $q->pool(parallel sub { return 42 } => 5); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Set your own queue | ||||||||||||||||||||||||||||
| 98 | $q->queue(parallel sub { return 42 } => 20); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | $q->consume(); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 METHODS | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | L<Mojo::IOLoop::ReadWriteProcess::Queue> inherits all methods from L<Mojo::Base> and implements | ||||||||||||||||||||||||||||
| 105 | the following new ones. | ||||||||||||||||||||||||||||
| 106 | Note: It proxies all the other methods of L<Mojo::IOLoop::ReadWriteProcess> for the whole process group. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 add | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | use Mojo::IOLoop::ReadWriteProcess qw(queue process); | ||||||||||||||||||||||||||||
| 111 | my $q = queue(); | ||||||||||||||||||||||||||||
| 112 | $q->add(sub { print "Hello 2! " }); | ||||||||||||||||||||||||||||
| 113 | $q->add(process sub { print "Hello 2! " }); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Add the process to the queue. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 consume | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | use Mojo::IOLoop::ReadWriteProcess qw(queue); | ||||||||||||||||||||||||||||
| 120 | my $q = queue(); | ||||||||||||||||||||||||||||
| 121 | $q->add(sub { print "Hello 2! " }); | ||||||||||||||||||||||||||||
| 122 | $q->add(process sub { print "Hello 2! " }); | ||||||||||||||||||||||||||||
| 123 | $q->consume; # executes and exhaust the processes | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Starts the processes and empties the queue. | ||||||||||||||||||||||||||||
| 126 | Note: maximum_processes can be set both to the pool (number of process to be run in parallel), | ||||||||||||||||||||||||||||
| 127 | and for the queue (that gets exhausted during the C<consume()> phase). | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | $q->pool->maximum_processes(2); # Max 2 processes in parallel | ||||||||||||||||||||||||||||
| 130 | $q->queue->maximum_processes(10); # Max queue is 10 | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 exhausted | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | use Mojo::IOLoop::ReadWriteProcess qw(queue); | ||||||||||||||||||||||||||||
| 135 | my $q = queue(); | ||||||||||||||||||||||||||||
| 136 | $q->add(sub { print "Hello 2! " }); | ||||||||||||||||||||||||||||
| 137 | $q->add(process sub { print "Hello 2! " }); | ||||||||||||||||||||||||||||
| 138 | $q->consume; # executes and exhaust the processes | ||||||||||||||||||||||||||||
| 139 | $q->exhausted; # 1 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Returns 1 if the queue is exhausted. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 ENVIRONMENT | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | You can set the MOJO_PROCESS_MAXIMUM_PROCESSES environment variable to specify the | ||||||||||||||||||||||||||||
| 146 | the maximum number of processes allowed in the pool and the queue, that are | ||||||||||||||||||||||||||||
| 147 | L<Mojo::IOLoop::ReadWriteProcess::Pool> instances. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | MOJO_PROCESS_MAXIMUM_PROCESSES=10000 | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Copyright (C) Ettore Di Giacinto. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 156 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Ettore Di Giacinto E<lt>edigiacinto@suse.comE<gt> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||