Forks/Queue.pm

filter: Statementstmt 67.0% 108 / 161 Branchbran 59.2% 45 / 76 Conditioncond 84.6% 22 / 26 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 58.7% 27 / 46 Podpod 88.4% 23 / 26 totaltotal 66.8% 232 / 347 CC 46 SCAR 15.7
CC
46
Cov
67%
shift_timed
12.0
pop_timed
12.0
limit
12.0
CRAP 4.8
« Forks/Queue/Shmem.pm ↑ Summary
1 package Forks::Queue;
2 121 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 121 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 121 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 121 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 121 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 121 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 121 our $VERSION = '0.14';
10 121 ▶our $DEBUG = $ENV{FORKS_QUEUE_DEBUG} || 0;
Condition50%1/2
$ENV{'FORKS_QUEUE_DEBUG'} || 0
l!l
0121
MC/DC0%0/1
$ENV{'FORKS_QUEUE_DEBUG'} || 0
$ENV{'FORKS_QUEUE_DEBUG'}
Truth table50%1/2
A
A= $ENV{'FORKS_QUEUE_DEBUG'}
Aresult
11
00
11  
12 121 ▶our $NOTIFY_OK = $ENV{FORKS_QUEUE_NOTIFY} // do {
Condition33%1/3
$ENV{"FORKS_QUEUE_NOTIFY"} // do { $Config::Config{"sig_name"} =~ /\bIO\b/ }
l!l&&r!l&&!r
01210
MC/DC0%0/2
$ENV{"FORKS_QUEUE_NOTIFY"} // do { $Config::Config{"sig_name"} =~ /\bIO\b/ }
$ENV{"FORKS_QUEUE_NOTIFY"} do { $Config::Config{"sig_name"} =~ /\bIO\b/ }
Truth table33%1/3
A // B
A= $ENV{"FORKS_QUEUE_NOTIFY"}B= do { $Config::Config{"sig_name"} =~ /\bIO\b/ }
ABresult
1X1
011
000
13 121 $Config::Config{sig_name} =~ /\bIO\b/;
14 };
15  
16 121 ▶our $SLEEP_INTERVAL = $NOTIFY_OK ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$Forks::Queue::NOTIFY_OK ? : 121 0 121
17 121 our $SLEEP_INTERVALX = 2;
18  
19 # default values to apply to all new Forks::Queue implementations
20 our %OPTS = (limit => -1, on_limit => 'fail', style => 'fifo',
21 121 ▶ impl => $ENV{FORKS_QUEUE_IMPL} || 'File' );
Condition50%1/2
$ENV{'FORKS_QUEUE_IMPL'} || 'File'
l!l
0121
MC/DC0%0/1
$ENV{'FORKS_QUEUE_IMPL'} || 'File'
$ENV{'FORKS_QUEUE_IMPL'}
Truth table50%1/2
A
A= $ENV{'FORKS_QUEUE_IMPL'}
Aresult
11
00
22  
23 sub new {
24 170 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=21.5
Pod documented
25 121 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 170 my %opts = (%OPTS, @_);
27  
28 170 ▶ if ($opts{impl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"impl"}) 169 1 170
29 169 my $pkg = delete $opts{impl};
30 169 ▶ if ($pkg =~ /[^\w:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /[^\w:]/) 0 169 169
31 0 croak "Forks::Queue cannot be instantiated. Invalid 'impl' $pkg";
32 }
33 169 my $e1 = eval "require Forks::Queue::$pkg; 1";
34 169 ▶ if ($e1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e1) 169 0 169
35 169 $pkg = "Forks::Queue::" . $pkg;
36 169 return $pkg->new(%opts);
37 }
38 0 my $err1 = $@;
39 0 ▶ if (eval "require $pkg; 1") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $pkg; 1") { } 0 0 0
40 0 return $pkg->new(%opts);
41 } else {
42 0 ▶ warn $err1 if $err1;
Branch: 1 branch
BranchTrueFalseTotal
if $err1 0 0 0
43 0 croak "Forks::Queue cannot be instantiated. ",
44 "Did not recognize 'impl' option '$opts{impl}'";
45 }
46 }
47 1 croak "Forks::Queue cannot be instantiated. ",
48 "Use an implementation or pass the 'impl' option to the constructor";
49 }
50  
51 sub import {
52 245 ▶ my ($class, @args) = @_;
Subroutine import: called CC=4 SCAR=23.2
53 245 ▶ for (my $i=0; $i<@args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 0 245 245
54 0 foreach my $key (qw(limit on_limit impl style)) {
55 0 ▶ if ($args[$i] eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[$i] eq $key) 0 0 0
56 0 $OPTS{$args[$i]} = $args[$i+1];
57 0 $i++;
58 0 next;
59 }
60 }
61 }
62 }
63  
64 sub put {
65 523 ▶ my $self = shift;
Subroutine put: called CC=1 SCAR=0.0
Pod documented
66 523 return $self->push(@_);
67 }
68  
69 sub enqueue {
70 0 ▶ my $self = shift;
Subroutine enqueue: not called CC=1 SCAR=6.9
Pod documented
71 0 return $self->push(@_);
72 }
73  
74 sub get {
75 161 ▶ my $self = shift;
Subroutine get: called CC=6 SCAR=17.9
Pod documented
76 161 ▶ _validate_input($_[0], 'count', 1) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 84 77 161
77 161 ▶ if ($self->{style} eq 'fifo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'style'} eq 'fifo') { } 133 28 161
78 133 ▶ return @_ ? $self->shift(@_) : $self->shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 70 63 133
79 } else {
80 28 ▶ my @gotten = @_ ? reverse($self->pop(@_)) : $self->pop;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 14 28
81 28 ▶ return @_ ? @gotten : $gotten[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 14 14 28
82 }
83 }
84  
85 0 ▶sub dequeue { goto &get; }
Subroutine dequeue: not called CC=1 SCAR=6.9
Pod documented
86  
87 sub dequeue_timed {
88 18 ▶ my $self = shift;
Subroutine dequeue_timed: called CC=3 SCAR=11.0
Pod documented
89 18 my $timeout = shift;
90 18 _validate_input($timeout, 'timeout', 0, 1);
91 18 local $self->{_expire} = Time::HiRes::time + $timeout;
92 18 local $SLEEP_INTERVAL = $Forks::Queue::SLEEP_INTERVAL;
93 18 ▶ $SLEEP_INTERVAL /= 2 while $SLEEP_INTERVAL > 0.25 * $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$Forks::Queue::SLEEP_INTERVAL > "0.25" * $timeout and do { $Forks::Queue::SLEEP_INTERVAL /= 2 } 12 18 30
94 18 ▶ return @_ ? $self->dequeue(@_) : $self->dequeue;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 18 0 18
95 }
96  
97 sub get_timed {
98 12 ▶ my $self = shift;
Subroutine get_timed: called CC=3 SCAR=11.0
Pod documented
99 12 my $timeout = shift;
100 12 _validate_input($timeout, 'timeout', 0, 1);
101 12 local $self->{_expire} = Time::HiRes::time + $timeout;
102 12 local $SLEEP_INTERVAL = $Forks::Queue::SLEEP_INTERVAL;
103 12 ▶ $SLEEP_INTERVAL /= 2 while $SLEEP_INTERVAL > 0.25 * $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$Forks::Queue::SLEEP_INTERVAL > "0.25" * $timeout and do { $Forks::Queue::SLEEP_INTERVAL /= 2 } 24 12 36
104 12 ▶ return @_ ? $self->get(@_) : $self->get;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 6 12
105 }
106  
107 sub shift_timed {
108 0 ▶ my $self = shift;
Subroutine shift_timed: not called CC=3 SCAR=24.8
Pod documented
109 0 my $timeout = shift;
110 0 _validate_input($timeout, 'timeout', 0, 1);
111 0 local $self->{_expire} = Time::HiRes::time + $timeout;
112 0 local $SLEEP_INTERVAL = $Forks::Queue::SLEEP_INTERVAL;
113 0 ▶ $SLEEP_INTERVAL /= 2 while $SLEEP_INTERVAL > 0.25 * $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$Forks::Queue::SLEEP_INTERVAL > "0.25" * $timeout and do { $Forks::Queue::SLEEP_INTERVAL /= 2 } 0 0 0
114 0 ▶ return @_ ? $self->shift(@_) : $self->shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
115 }
116  
117 sub pop_timed {
118 0 ▶ my $self = shift;
Subroutine pop_timed: not called CC=3 SCAR=24.8
Pod documented
119 0 my $timeout = shift;
120 0 _validate_input($timeout, 'timeout', 0, 1);
121 0 local $self->{_expire} = Time::HiRes::time + $timeout;
122 0 local $SLEEP_INTERVAL = $Forks::Queue::SLEEP_INTERVAL;
123 0 ▶ $SLEEP_INTERVAL /= 2 while $SLEEP_INTERVAL > 0.25 * $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$Forks::Queue::SLEEP_INTERVAL > "0.25" * $timeout and do { $Forks::Queue::SLEEP_INTERVAL /= 2 } 0 0 0
124 0 ▶ return @_ ? $self->pop(@_) : $self->pop;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
125 }
126  
127 sub _expired {
128 40342 ▶ my ($self) = @_;
Subroutine _expired: called CC=2 SCAR=6.9
129 40342 ▶ $self->{_expire} && Time::HiRes::time >= $self->{_expire};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_expire"} and &Time::HiRes::time() >= $self->{"_expire"} 40307 35 40342
130 }
131  
132 sub get_nb {
133 18 ▶ my $self = shift;
Subroutine get_nb: called CC=5 SCAR=18.6
Pod documented
134 18 ▶ _validate_input($_[0], 'count', 1) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 15 3 18
135 3 ▶ if ($self->{style} eq 'fifo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'style'} eq 'fifo') { } 0 3 3
136 0 ▶ return @_ ? $self->shift_nb(@_) : $self->shift_nb;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
137 } else {
138 3 ▶ return @_ ? @ := reverse($self->pop_nb(@_)) : $self->pop_nb;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
139 }
140 }
141 15 ▶sub dequeue_nb { goto &get_nb; }
Subroutine dequeue_nb: called CC=1 SCAR=0.0
Pod documented
142  
143 sub peek {
144 129 ▶ my ($self,$index) = @_;
Subroutine peek: called CC=5 SCAR=16.1
Pod documented
145 129 ▶ _validate_input($index, 'index') if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 111 18 129
146 120 ▶ if ($self->{style} eq 'lifo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'style'} eq 'lifo') { } 48 72 120
147 48 ▶ return $self->peek_back($index || 0);
Condition100%2/2
$index || 0
l!l
3018
MC/DC100%1/1
$index || 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
148 } else {
149 72 ▶ return $self->peek_front($index || 0);
Condition100%2/2
$index || 0
l!l
639
MC/DC100%1/1
$index || 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
150 }
151 }
152  
153 sub pending {
154 266 ▶ my $self = shift;
Subroutine pending: called CC=3 SCAR=11.1
Pod documented
155 266 my $s = $self->status;
156 266 ▶ return $s->{avail} ? $s->{avail} : $s->{end} ? undef : 0;
Branch: 2 branches
BranchTrueFalseTotal
$s->{'avail'} ? : 132 134 266
$s->{'end'} ? : 0 134 134
157 }
158  
159 sub _unimpl {
160 0 ▶ my $func = shift;
Subroutine _unimpl: not called CC=1 SCAR=6.9
161 0 croak "Forks::Queue: $func not implemented in abstract base class";
162 }
163  
164 sub limit :lvalue {
165 0 ▶ my $self = shift;
Subroutine limit: not called CC=3 SCAR=24.8
Pod documented
166 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
167 0 $self->{limit} = shift @_;
168 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
169 0 $self->{on_limit} = shift @_;
170 }
171 }
172 0 $self->{limit};
173 }
174  
175 sub _validate_input {
176 430 ▶ my ($value,$name,$ge,$float_ok) = @_;
Subroutine _validate_input: called CC=7 SCAR=19.5
177 430 ▶ croak "Invalid '$name'"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value && &looks_like_number($value) or not $float_ok and $value != int $value or defined $ge and $value < $ge 36 355 391
Condition100%15/15
not defined $value && &looks_like_number($value) or not $float_ok and $value != int $value or defined $ge and $value < $ge
l!l&&r!l&&!r
5732341
not defined $value && &looks_like_number($value) or not $float_ok and $value != int $value
l!l&&r!l&&!r
3918373
defined $value && &looks_like_number($value)
!ll&&!rl&&r
2118391
not $float_ok and $value != int $value
!ll&&!rl&&r
3034318
defined $ge and $value < $ge
!ll&&!rl&&r
19516018
MC/DC83%5/6
not defined $value && &looks_like_number($value) or not $float_ok and $value != int $value or defined $ge and $value < $ge
defined $value &looks_like_number($value) not $float_ok $value != int $value defined $ge $value < $ge
Truth table66%6/9
not(A && B) or not(C) and D or E and F
A= defined $valueB= &looks_like_number($value)C= not $float_okD= $value != int $valueE= defined $geF= $value < $ge
ABCDEFresult
0XXXXX1
10XXXX1
1111XX1
110X111
1110111
110X0X0
110X100
11100X0
1110100
178 if !defined($value) ||
179 !looks_like_number($value) ||
180 (!$float_ok && $value != int($value)) ||
181 (defined($ge) && $value < $ge);
182 355 return $value;
183 }
184  
185 0 ▶sub push { _unimpl("push/put") }
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
186 0 ▶sub peek_front { _unimpl("peek") }
Subroutine peek_front: not called CC=1 SCAR=6.9
Pod undocumented
187 0 ▶sub peek_back { _unimpl("peek") }
Subroutine peek_back: not called CC=1 SCAR=6.9
Pod undocumented
188 0 ▶sub shift :method { _unimpl("shift/get") }
Subroutine shift: not called CC=1 SCAR=6.9
Pod documented
189 0 ▶sub unshift { _unimpl("unshift") }
Subroutine unshift: not called CC=1 SCAR=6.9
Pod documented
190 0 ▶sub pop { _unimpl("pop/get") }
Subroutine pop: not called CC=1 SCAR=6.9
Pod documented
191 0 ▶sub shift_nb { _unimpl("shift/get") }
Subroutine shift_nb: not called CC=1 SCAR=6.9
Pod documented
192 0 ▶sub pop_nb { _unimpl("pop/get") }
Subroutine pop_nb: not called CC=1 SCAR=6.9
Pod documented
193 0 ▶sub status { _unimpl("pending/status") }
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
194 0 ▶sub clear { _unimpl("clear") }
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
195 0 ▶sub end { _unimpl("end") }
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
196 0 ▶sub lock :method { _unimpl("lock") }
Subroutine lock: not called CC=1 SCAR=6.9
Pod undocumented
197  
198  
199  
200  
201  
202 sub Forks::Queue::Util::__is_nfs {
203 198 ▶ my ($dir) = @_;
Subroutine __is_nfs: called CC=3 SCAR=11.2
204 198 ▶ if ($^O ne 'linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "linux") 0 198 198
205 0 return;
206 }
207 198 my $pid = fork();
208 198 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 48 150 198
209 48 close STDOUT;
210 48 close STDERR;
211 # http://superuser.com/q/422061
212 48 exec("df",$dir,"-t","nfs");
213 0 die;
214 }
215 150 local $?;
216 150 waitpid $pid,0;
217 150 return $? == 0;
218 }
219  
220 sub Forks::Queue::Util::QID {
221 121 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
222 135 ▶ if ($Forks::Queue::Util::PID != $$) {
Subroutine QID: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($Forks::Queue::Util::PID != $$) 85 50 135
223 85 $Forks::Queue::Util::PID = $$;
224 85 $Forks::Queue::Util::QID = 0;
225 }
226 135 $Forks::Queue::Util::QID++;
227 135 join("-", $Forks::Queue::Util::PID, $Forks::Queue::Util::QID);
228 }
229  
230  
231 # manage global destruction phase
232 BEGIN {
233 121 ▶ if (defined(${^GLOBAL_PHASE})) {
Subroutine BEGIN: called CC=2 SCAR=8.5
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${^GLOBAL_PHASE}) { } 121 0 121
234 121 ▶ eval 'sub __inGD(){%{^GLOBAL_PHASE} eq q{DESTRUCT} && __END()};1'
Subroutine __inGD: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
%{^GLOBAL_PHASE} eq "DESTRUCT" and __END() 0 15847 15847
235 } else {
236 0 require B;
237 0 eval 'sub __inGD(){${B::main_cv()}==0 && __END()};1'
238 }
239 }
240 73 ▶END { &__END }
Subroutine END: called CC=1 SCAR=0.0
241 sub __END {
242 121 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 73 ▶ *DB::DB = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
Subroutine __END: called CC=1 SCAR=0.0
244 73 *__inGD = sub () { 1 };
245 }
246  
247  
248  
249 121 1;
250  
251 =head1 NAME
252  
253 Forks::Queue - queue that can be shared safely across processes
254  
255 =head1 VERSION
256  
257 0.14
258  
259 =head1 SYNOPSIS
260  
261 use Forks::Queue;
262 $q = Forks::Queue->new( impl => ..., style => 'lifo' );
263  
264 # put items on queue
265 $q->put("a scalar item");
266 $q->put(["an","arrayref","item"]);
267 $q->put({"a"=>"hash","reference"=>"item"});
268 $q->put("list","of","multiple",["items"]);
269 $q->end; # no more jobs will be added to queue
270  
271 # retrieve items from queue, possibly after a fork
272 $item = $q->get;
273 $item = $q->peek; # get item without removing it
274 @up_to_10_items = $q->get(10);
275 $remaining_items = $q->pending;
276  
277 =head1 DESCRIPTION
278  
279 Interface for a queue object that can be shared across processes
280 and threads.
281 Available implementations are L<Forks::Queue::File|Forks::Queue::File>,
282 L<Forks::Queue::Shmem|Forks::Queue::Shmem>,
283 L<Forks::Queue::SQLite|Forks::Queue::SQLite>.
284  
285 =head1 METHODS
286  
287 Many of these methods pass or return "items". For this distribution,
288 an "item" is any scalar or reference that can be serialized and
289 shared across processes.
290  
291 This will include scalars and most unblessed references
292  
293 "42"
294 [1,2,3,"forty-two"]
295 { name=>"a job", timestamp=>time, input=>{foo=>[19],bar=>\%bardata} }
296  
297 but will generally preclude data with blessed references and code references
298  
299 { name => "bad job", callback => \&my_callback_routine }
300 [ 46, $url13, File::Temp->new ]
301  
302 Many of the methods of C<Forks::Queue> have analogues in the
303 L<Thread::Queue> class, and many scripts using L<Thread::Queue>
304 can be easily transformed to use C<Forks::Queue>.
305  
306 =head2 new
307  
308 $queue = Forks::Queue->new( %opts )
309  
310 Instantiates a new queue object with the given configuration.
311  
312 If one of the options is C<impl>, the constructor from that
313 C<Forks::Queue> subclass will be invoked.
314  
315 Other options that should be supported on all implementations include
316  
317 =over 4
318  
319 =item * style
320  
321 =item * C<< style => 'fifo' | 'lifo' >>
322  
323 Indicates whether the L<"get"> method will return items in
324 first-in-first-out order or last-in-first-out order (and which
325 end of the queue the L<"peek"> method will examine)
326  
327 =item * limit
328  
329 =item * C<< limit => int >>
330  
331 A maximum size for the queue. Set to a non-positive value to
332 specify an unlimited size queue.
333  
334 =item * on_limit
335  
336 =item * C<< on_limit => 'block' | 'fail' | 'tq-compat' >>
337  
338 Dictates what the queue should do when an attempt is made to
339 add items beyond the queue's limit. If C<block>, the queue
340 will block and wait until items are removed from the queue.
341 If C<fail>, the queue will warn and return immediately without
342 changing the queue.
343  
344 The setting C<tq-compat> is similar to C<block>, but has the
345 additional effect where the L<"insert"> method operates without
346 regard to the queue limit. This behavior is compatible with
347 the way queue limits and the insert method work in the
348 L<Thread::Queue> package.
349  
350 See the L<"enqueue">, L<"put">, L<"enqueue">, L<"push">, L<"unshift">,
351 and L<"insert"> methods, which increase the length
352 of the queue and may be affected by this setting.
353  
354 =item * join
355  
356 =item * C<< join => bool >>
357  
358 If true, expects that the queue referred to by this constructor
359 has already been created in another process, and that the current
360 process should access the existing queue. This allows a queue to
361 be shared across unrelated processes (i.e., processes that do not
362 have a parent-child relationship).
363  
364 # my_daemon.pl - may run "all the time" in the background
365 $q = Forks::Queue::File->new(file=>'/var/spool/foo/q17');
366 # creates new queue object
367 ...
368  
369 # worker.pl - may run periodically for a short time, launched from
370 # cron or from command line, but not from the daemon
371 $q = Forks::Queue->new( impl => 'File', join => 1,
372 file => '/var/spool/foo/q17',
373 # the new queue attaches to existing file at /var/spool/foo/q17
374 ...
375  
376 C<join> is not necessary for child processes forked from a process with
377 an existing queue
378  
379 $q = Forks::Queue->new(...)
380 ...
381 if (fork() == 0) {
382 # $q already exists and the child process can begin using it,
383 # no need for a Forks::Queue constructor with join
384 ...
385 }
386  
387 =item * persist
388  
389 =item * C<< persist => bool >>
390  
391 Active C<Forks::Queue> objects affect your system, writing to disk or
392 writing to memory, and in general they clean themselves up when they
393 detect that no more processes are using the queue. The C<persist> option,
394 if set to true, instructs the queue object to leave its state intact
395 after destruction.
396  
397 An obvious use case for this option is debugging, to examine the
398 state of the queue after abnormal termination of your program.
399  
400 A second use case is to create persistent queues -- queues that are
401 shared not only among different processes, but among different
402 processes that are running at different times. The persistent queue
403 can be used by supplying both the C<persist> and the C<join> options
404 to the C<Forks::Queue> constructor.
405  
406 $queue_file = "/tmp/persistent.job.queue";
407 $join = -f $queue_file;
408 $q = Forks::Queue->new( impl => 'File', file => $queue_file,
409 join => $join, persist => 1 );
410 ... work with the queue ...
411 # the queue remains intact if this program exits or aborts
412  
413 =item * C<< list => ARRAYREF >>
414  
415 Initializes the contents of the queue with the argument to the
416 C<list> option. The argument must be an array reference.
417  
418 If the C<join> option is specified, the contents of the list
419 could be added to an already existing queue.
420  
421 =back
422  
423 See the global L<"%OPTS"> variable for information about
424 default values for many of these settings.
425  
426 =head2 put
427  
428 =head2 enqueue
429  
430 $count = $queue->put(@items);
431 $count = $queue->enqueue(@items)
432  
433 Place one or more "items" on the queue, and returns the number of
434 items successfully added to the queue.
435  
436 Adding items to the queue will fail if the L<"end"> method of
437 the queue had previously been called from any process.
438  
439 See the L<"limit"> method to see how the C<put> method behaves
440 when adding items would cause the queue to exceed its maximum size.
441  
442 The C<enqueue> method name is provided for compatibility with
443 L<Thread::Queue|Thread::Queue>.
444  
445  
446  
447 =head2 push
448  
449 $count = $queue->push(@items)
450  
451 Equivalent to L<"put">, adding items to the end of the queue and
452 returning the number of items successfully added. The most recent
453 items appended to the queue by C<push> or C<put> will be the first
454 items taken from the queue by L<"pop"> or by L<"get"> with LIFO
455 style queues, and the last items removed by L<"shift"> or L<"get">
456 with FIFO style queues.
457  
458 If the items added to the queue would cause the queue to exceed
459 its queue size limit (as determined by the L<"limit"> attribute),
460 this method will either block until queue capacity is available,
461 or issue a warning about the uninserted items and return the
462 number of items added, depending on the queue's setting for
463 L<"on_limit"|Forks::Queue/"new">.
464  
465  
466 =head2 unshift
467  
468 $count = $queue->unshift(@items)
469  
470 Equivalent to C<insert(0,@items)>, adding items to the front
471 of the queue, and returning the number of items successfully
472 added. In FIFO queues, items added to the queue with C<unshift>
473 will be the last items taken from the queue by L<"get">,
474 and in LIFO queues, they will be the first items taken from the
475 queue by L<"get">.
476  
477 This method is inefficient for some queue implementations.
478  
479 =head2 end
480  
481 $queue->end
482  
483 Indicates that no more items are to be put on the queue,
484 so that when a process tries to retrieve an item from an empty queue,
485 it will not block and wait for new items to be added. Causes any
486 processes blocking on a L<"get">/L<"dequeue">/L<"shift">/L<"pop">
487 call to become unblocked and return C<undef>.
488 This method may be called from any process that has access to the queue.
489  
490 Calling C<end> on a queue more than once will generate a warning
491 message, even if the caller is not the same process/thread that
492 made the original C<end> call.
493  
494  
495 =head2 get
496  
497 =head2 dequeue
498  
499 $item = $queue->get;
500 $item = $queue->dequeue;
501  
502 @items = $queue->get($count);
503 @items = $queue->dequeue($count);
504  
505 Attempt to retrieve one or more "items" on the queue. If the
506 queue is empty, and if L<"end"> has not been called on the queue,
507 this call blocks until an item is available or until the L<"end">
508 method has been called from some other process. If the queue is
509 empty and L<"end"> has been called, this method returns an
510 empty list in list context or C<undef> in scalar context.
511  
512 If a C<$count> argument is supplied, returns up to C<$count> items
513 or however many items are currently availble on the queue, whichever
514 is fewer. But the call still blocks if L<"end"> has not been called
515 until there is at least one item available. See L<"get_nb"> for a
516 non-blocking version of this method. The return value of this
517 function when a C<$count> argument is supplied is always a list,
518 so if you evaluate it in scalar context you will get the number of items
519 retrieved from the queue, not the items themselves.
520  
521 $job = $q->get; # $job is an item from the queue
522 $job = $q->get(1); # returns # of items retrieved, not an actual item!
523 ($job) = $q->get(1); # $job is an item from the queue
524  
525 The only important difference between C<get> and C<dequeue> is what
526 happens when there is a C<$count> argument, and the queue currently has
527 more than zero but fewer than C<$count> items available. In this case,
528 the C<get> call will return all of the available items. The C<dequeue>
529 method will block until at least C<$count> items are available on the
530 queue, or until the L<"end"> method has been called on the queue.
531 This C<dequeue> behavior is consistent with the behavior of the
532 L<"dequeue" method in Thread::Queue|Thread::Queue/"dequeue">.
533  
534  
535 =head2 pop
536  
537 $item = $queue->pop
538 @items = $queue->pop($count)
539  
540 Retrieves one or more items from the "back" of the queue.
541 For LIFO style queues, the L<"get"> method is equivalent to this method.
542 Like C<"get">, this method blocks while the queue is empty and the
543 L<"end"> method has not been called on the queue.
544  
545 If a C<$count> argument is supplied, returns up to C<$count> items or however
546 many items are currently available on the queue, whichever is fewer.
547 (Like the L<"get"> call, this method blocks when waiting for input. See
548 L<"pop_nb"> for a non-blocking version of the method. Also like
549 L<"get">, you should be wary of using this method in scalar context
550 if you provide a C<$count> argument).
551  
552 =head2 shift
553  
554 $item = $queue->shift
555 @items = $queue->shift($count)
556  
557 Retrieves one or more items from the "front" of the queue.
558 For FIFO style queues, the L<"get"> method is equivalent to this method.
559 Like C<"get">, this method blocks while the queue is empty and the
560 L<"end"> method has not been called on the queue.
561  
562 If a C<$count> argument is supplied, returns up to C<$count> items or however
563 many items are currently available on the queue, whichever is fewer. (Like the
564 L<"get"> call, this method blocks when waiting for input. See
565 L<"shift_nb"> for a non-blocking version of the method. Also like
566 L<"get">, you should be wary of using this method in scalar context
567 if you provide a C<$count> argument).
568  
569  
570 =head2 get_nb
571  
572 =head2 dequeue_nb
573  
574 =head2 pop_nb
575  
576 =head2 shift_nb
577  
578 $item = $queue->XXX_nb
579 @items = $queue->XXX_nb($count)
580  
581 Non-blocking versions of the L<"get">, L<"dequeue">, L<"pop">,
582 and L<"shift"> methods. These functions return immediately if
583 there are no items in the queue to retrieve, returning C<undef>
584 in the case with no arguments and an empty list when a
585 C<$count> argument is supplied.
586  
587 =head2 get_timed
588  
589 =head2 dequeue_timed
590  
591 =head2 shift_timed
592  
593 =head2 pop_timed
594  
595 $item = $queue->XXX_timed($timeout)
596 @item = $queue->XXX_timed($timeout,$count)
597  
598 Timed versions of L<"get">, L<"dequeue">, L<"shift">, and L<"pop">
599 that take a C<$timeout> argument and will stop blocking after
600 C<$timeout> seconds have elapsed.
601  
602 If a C<$count> argument is supplied to C<dequeue_timed>, the function
603 will wait up to C<$timeout> seconds for at least C<$count> items to
604 be available on the queue. After C<$timeout> seconds have passed,
605 the function will return up to C<$count> available items.
606  
607 For other timed methods, supplying a C<$count> argument for a
608 queue with more than zero but fewer than C<$count> items available
609 will return all available items without blocking.
610  
611  
612 =head2 peek
613  
614 $item = $queue->peek
615 $item = $queue->peek($index)
616 $item = $queue->peek_front
617 $item = $queue->peek_back
618  
619 Returns an item from the queue without removing it. The C<peek_front>
620 and C<peek_back> methods inspect the item at the front and the back of
621 the queue, respectively. The generic C<peek> method is equivalent to
622 C<peek_front> for FIFO style queues and C<peek_back> for LIFO style
623 queues. If an index is specified, returns the item at that position
624 in the queue (where position 0 is the head of the queue). Negative
625 indices are supported, so a call to C<< $queue->peek(-2) >>,
626 for example, would return the second to last item in the queue.
627  
628 If the queue is empty or if the specified index is larger than the
629 number of elements currently in the queue, these methods will
630 return C<undef> without blocking.
631  
632 Note that unlike the
633 L<< "peek" method in C<Thread::Queue>|Thread::Queue/"peek" >>,
634 C<Forks::Queue::peek> returns a copy of the item on the queue,
635 so manipulating a reference returned from C<peek> while B<not>
636 affect the item on the queue.
637  
638 =head2 extract
639  
640 $item = $queue->extract
641 $item = $queue->extract($index)
642 @items = $queue->extract($index,$count)
643  
644 Removes and returns the specified number of items from the queue
645 at the specified index position, to provide random access to the
646 queue. The method is non-blocking and may return fewer than the
647 number of items requested (or zero items) if there are not enough
648 items in the queue to satisfy the request.
649  
650 If the C<$count> argument is not provided, the method will return
651 (if available) a single item. If the C<$index> argument is also
652 not provided, it will return the first item on the queue exactly
653 like the L<"get_nb"> method with no arguments.
654  
655 Negative C<$index> values are supported, in which case this
656 method will extract the corresponding items at the back of the
657 queue.
658  
659 Like C<get()> vs. C<get($count)>, the return value is always a
660 scalar when no C<$count> argument is provided, and always a list
661 when it is.
662  
663  
664 =head2 insert
665  
666 $count = $queue->insert($index, @list)
667  
668 Provides random access to the queue, inserting the items specified
669 in C<@list> into the queue after index position C<$index>.
670 Negative C<$index> values are supported, which indicate that the
671 items should be inserted after that position relative to the
672 back of the queue. Returns the number of items that were
673 inserted into the queue.
674  
675 If the queue has a L<"limit"> set, and inserting all the items on
676 the list would cause the queue size to exceed the limit, the setting
677 of the queue's C<"on_limit">
678 parameter will govern how this method will behave.
679 See the L<"on_limit"|Forks::Queue/"new"> setting for details.
680  
681 This method is inefficient for some queue implementations.
682  
683  
684 =head2 pending
685  
686 $num_items_avail = $queue->pending
687  
688 Returns the total number of items available on the queue. There is no
689 guarantee that the number of available items will not change between a
690 call to C<pending> and a subsequent call to L<"get">
691  
692 =head2 clear
693  
694 $queue->clear
695  
696 Removes all items from the queue.
697  
698  
699 =head2 status
700  
701 $status = $queue->status
702  
703 Returns a hash reference with meta information about the queue.
704 The information should at least include the number of items remaining in
705 the queue. Other implementations may provide additional information
706 in this return value.
707  
708  
709 =head2 limit
710  
711 $max = $queue->limit
712 $queue->limit($new_limit)
713 $queue->limit($new_limit,$on_limit)
714 $queue->limit = $new_limit # limit as lvalue requires Perl >=v5.14
715  
716 Returns or updates the maximum size of the queue. With no args, returns
717 the existing maximum queue size, with a non-positive value indicating
718 that the queue does not have a maximum size.
719  
720 The return value also acts as an lvalue through which the maximum
721 queue size can be set, and allows the C<limit> method to be used
722 in the same way as
723 L<< the C<limit> method in Thread::Queue|Thread::Queue/"limit" >>.
724 I<< Note: lvalue feature rqeuires Perl v5.14 or better. >>
725  
726 If arguments are provided, the first argument is used to set the
727 maximum queue size. A non-positive queue size can be specified to
728 indicate that the queue does not have a maximum size.
729
730 The second argument, if provided, updates the behavior of the queue
731 when an attempt is made to add items beyond the maximum size.
732 See L<"on_limit"|Forks::Queue/"new"> for the recognized values
733 of this argument and how they affect the behavior of the
734 L<"put">/L<"push">/L<"enqueue">, L<"unshift">,
735 L<"insert">, and L<"dequeue"> methods.
736  
737  
738 =head1 VARIABLES
739  
740 =head2 %OPTS
741  
742 Global hash containing the set of default options for all
743 C<Forks::Queue> constructors. Initially this hash contains the
744 key-value pairs
745  
746 impl "File"
747 style "fifo"
748 limit -1
749 on_limit "fail"
750  
751 but they may be changed at any time to affect all subsequently
752 constructed C<Forks::Queue> objects. The global options can also
753 be set at import time with additional arguments for the C<use>
754 statement.
755  
756 use Forks::Queue impl => 'SQLite'; # use SQLite queues by default
757 $Forks::Queue::OPTS{impl} = 'SQLite'; # equivalent run-time call
758
759 use Forks::Queue
760 on_limit => 'block', limit => 10; # finite, blocking queues by default
761 $Forks::Queue::OPTS{limit} = 10;
762 $Forks::Queue::OPTS{on_limit} = 'block'; # equivalent run-time calls
763  
764 =head1 ENVIRONMENT
765  
766 Some environment variable settings that can affect this module:
767  
768 =over 4
769  
770 =item * FORKS_QUEUE_IMPL
771  
772 Specifies a default implementation to use, overriding the initial setting
773 of C<$Forks::Queue::OPTS{"impl"}>, in cases where the C<Forks::Queue>
774 constructor is invoked without passing an C<impl> option.
775  
776 =item * FORKS_QUEUE_DEBUG
777  
778 If set to a true value, outputs information about the activity of
779 the queues to standard error.
780  
781 =item * FORKS_QUEUE_NOTIFY
782  
783 If set to a false value, disables use of signals on POSIX-y platforms
784 that may help improve queue performance
785  
786 =item * FORKS_QUEUE_DIR
787  
788 Specifies a directory to use for temporary queue files in the
789 L<File|Forks::Queue::File> and
790 L<SQLite|Forks::Queue::SQLite> implementations.
791 If this directory is not specified, the implementations will try to make
792 a reasonable choice based on your platform and other environment settings.
793  
794 =back
795  
796 =head1 DEPENDENCIES
797  
798 The C<Forks::Queue> module and all its current implementations require
799 the L<JSON|JSON> module.
800  
801 =head1 SEE ALSO
802  
803 L<Thread::Queue|Thread::Queue>, L<File::Queue|File::Queue>,
804 L<Queue::Q|Queue::Q>, L<MCE::Queue|MCE::Queue>,
805 L<Queue::DBI|Queue::DBI>, L<Directory::Queue|Directory::Queue>.
806  
807 =head1 SUPPORT
808  
809 You can find documentation for this module with the perldoc command.
810  
811 perldoc Forks::Queue
812  
813  
814 You can also look for information at:
815  
816 =over 4
817  
818 =item * RT: CPAN's request tracker (report bugs here)
819  
820 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Forks-Queue>
821  
822 =item * CPAN Ratings
823  
824 L<http://cpanratings.perl.org/d/Forks-Queue>
825  
826 =item * Search CPAN
827  
828 L<http://search.cpan.org/dist/Forks-Queue/>
829  
830 =back
831  
832  
833 =head1 LICENSE AND COPYRIGHT
834  
835 Copyright (c) 2017-2019, Marty O'Brien.
836  
837 This library is free software; you can redistribute it and/or modify
838 it under the same terms as Perl itself, either Perl version 5.10.1 or,
839 at your option, any later version of Perl 5 you may have available.
840  
841 See http://dev.perl.org/licenses/ for more information.
842  
843 =cut
844  
845 # TODO:
846 #
847 # priorities
848 # Directory implementation (see Queue::Dir)
849 # Distinguish enqueue and put . enqueue should behave like
850 # Thread::Queue and only check the limit once
« Forks/Queue/Shmem.pm ↑ Summary