lib/Mojo/IOLoop/ReadWriteProcess/Queue.pm

filter: Statementstmt 100% 56 / 56 Branchbran 85.7% 12 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 3 / 3 totaltotal 93.0% 94 / 101 CC 10 SCAR 6.4
CC
10
Cov
95%
_dequeue
3.0
__ANON__
3.0
AUTOLOAD
3.0
CRAP 1.9
« 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
BranchTrueFalseTotal
if $self->queue->first and $self->pool->add($self->queue->first) 32 8 40
Condition66%2/3
$self->queue->first and $self->pool->add($self->queue->first)
!ll&&!rl&&r
8032
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$_[0]->pool->size == 0 and (shift())->queue->size == 0 4 900 904
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
BranchTrueFalseTotal
unless ($self->exhausted) 900 4 904
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
BranchTrueFalseTotal
unless $p 0 1038 1038
38 1038 ▶ return if exists $p->{started};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{"started"} 998 40 1038
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(@_)
l!l&&r!l&&!r
8320
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(@_)
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $fn eq "DESTROY" 1 3 4
57 3 my $self = shift;
58 return (
59 3 ▶ eval { $self->pool->Mojo::IOLoop::ReadWriteProcess::Pool::_cmd(@_, $fn) },
Branch: 1 branch
BranchTrueFalseTotal
grep(/once|on|emit/u, $fn) ? : 3 0 3
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
« lib/Mojo/IOLoop/ReadWriteProcess/CGroup/v1.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Pool.pm »