blib/lib/IPC/Concurrency.pm

filter: Statementstmt 100% 33 / 33 Branchbran 87.5% 14 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 96.7% 60 / 62 CC 11 SCAR 16.6
CC
11
Cov
96%
get_slot
8.0
new
4.0
BEGIN
1.0
CRAP 5.3
↑ Summary
1 package IPC::Concurrency;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use IPC::ShareLite ':lock';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 IPC::Concurrency - Concurrency guard for processes.
12  
13 =head1 VERSION
14  
15 Version 0.5
16  
17 =cut
18  
19 3 our $VERSION = '0.5';
20  
21 =head1 SYNOPSIS
22  
23 This module allows you to specify how many processes of given kind you want to run in parallel.
24  
25 May be usefull when you want to prevent machine overload or provide exclusive access to some resource.
26  
27 This is NOT a forker.
28  
29 use IPC::Concurrency;
30
31 my $name = 'PROC';
32 my $c = IPC::Concurrency->new($name);
33
34 # your process will end if there are already 4 processes registered as 'PROC' running
35 exit unless $c->get_slot(4);
36
37 # otherwise it will run as usual
38 do_some_tasks();
39  
40 =head2 System requirements
41  
42 Your system must support SysV IPC shared memory and semaphores as well as kill command.
43  
44 If you pass the test suite for L<IPC::ShareLite> then you're ready to go :)
45  
46 =head2 Containers
47  
48 Containers are used to name and group processes (like 'PROC' in SYNOPSIS).
49 They are located in shared memory and are accessible by any user.
50  
51 Containers must be named as exactly 4 characters from A-Z range (uppercase).
52  
53 =head1 FAQ
54  
55 B<Q:> Can i change $0 variable?
56  
57 B<A:> Yes. You can change $0 variable during runtime if you want.
58 Finding amount of running processes of given kind is totally 'ps ux' independent.
59  
60 --
61  
62 B<Q:> Can i use the same $name for processes running on different users?
63  
64 B<A:> Yes. For example you can restrict them to access some shared device one at a time.
65  
66 Example:
67  
68 package Scanner::GUI;
69
70 use IPC::Concurrency;
71
72 my $c = IPC::Concurrency->new('SCNR');
73
74 while (not $c->get_slot(1)) {
75 print 'Scanner is busy', $/;
76 sleep 4;
77 }
78
79 run_scanner_gui();
80  
81 This code will wait till every other 'SCNR' processes are not active.
82 It's much more simple approach than grepping 'ps aux' table or creating lockfiles.
83  
84 --
85  
86 B<Q:> Can i register my process under many names?
87  
88 B<A:> Yes. For example you may want to run no more that 4 parsers and no more than 32 processes on some machine.
89  
90 Example:
91  
92 package Parser;
93
94 use IPC::Concurrency;
95
96 my $c1 = IPC::Concurrency->new('PARS');
97 my $c2 = IPC::Concurrency->new('GLOB');
98
99 exit unless $c1->get_slot(4) and $c2->get_slot(32);
100  
101 --
102  
103 B<Q:> What is the limit for number of containers?
104  
105 B<A:> You can make as many containers as your system allows. You will get C<< Carp::confess('No space left on device') >> if you exceed available memory or semaphores.
106  
107 --
108  
109 B<Q:> What is the limit for number of processes registered in one container?
110  
111 B<A:> You can request C<< $c2->get_slot(1024) >> max.
112  
113 --
114  
115 B<Q:> Can i use this module for limiting child processes?
116  
117 B<A:> Yes. In the following example child process doesn't know
118 how many other child processes have been spawned. But it can use get_slot()
119 to prevent exceeding 10 live child processes.
120  
121 Example:
122  
123 package Scraper;
124
125 use IPC::Concurrency;
126
127 my $c1 = IPC::Concurrency->new('SCRA');
128
129 unless ( my $pid = fork() ) {
130 exit unless $c1->get_slot(10);
131 }
132  
133  
134 =head1 FUNCTIONS
135  
136 =head2 new
137  
138 my $name = 'PROC';
139 my $c = IPC::Concurrency->new($name);
140  
141 Creates new object and allocates Shared Memory container under C<< $name >>.
142  
143 L<Carp::confess> will be called on failure.
144  
145 =cut
146  
147 5 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
148 5 my ( $class, $name ) = @_;
149 5 my $self = {};
150  
151 5 ▶ confess 'Name missing' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 4 5
152 4 ▶ confess 'Name must be exactly 4 characters from A-Z range (uppercase)'
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[A-Z]{4}$/ 1 3 4
153 unless $name =~ m/^[A-Z]{4}$/;
154  
155 3 ▶ $self->{'concurrency'} = IPC::ShareLite->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->{"concurrency"} = "IPC::ShareLite"->new("-key", $name, "-create", "yes", "-destroy", "no") or confess($!) 3 0 3
156 '-key' => $name,
157 '-create' => 'yes',
158 '-destroy' => 'no'
159 ) or confess $!;
160  
161 3 return bless $self, $class;
162 }
163  
164 =head2 get_slot
165  
166 $count = 1;
167 exit unless $c->get_slot($count);
168  
169 Request slot. You will get it if there are no more than C<< $count - 1 >> processes registered under given C<< $name >>.
170  
171 =cut
172  
173 8 ▶sub get_slot {
Subroutine get_slot: called CC=8 SCAR=20.8
Pod documented
174 8 my ( $self, $count ) = @_;
175  
176 8 ▶ confess 'Count missing' unless defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 1 7 8
177 7 ▶ confess 'Count should be positive integer' unless $count =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $count =~ /^\d+$/ 2 5 7
178 5 ▶ confess 'Count should be in 1..1024 range'
Branch: 1 branch
BranchTrueFalseTotal
unless $count >= 1 and $count <= 1024 2 3 5
Condition100%3/3
$count >= 1 and $count <= 1024
!ll&&!rl&&r
113
MC/DC100%2/2
$count >= 1 and $count <= 1024
$count >= 1 $count <= 1024
Truth table100%3/3
A and B
A= $count >= 1B= $count <= 1024
ABresult
0X0
100
111
179 unless $count >= 1
180 and $count <= 1024;
181  
182 3 $self->{'concurrency'}->lock( LOCK_EX );
183  
184 3 my %pids = map { $_ => 1 } split /,/, $self->{'concurrency'}->fetch();
185  
186 3 for my $pid ( keys %pids ) {
187 2 ▶ not kill 0, $pid and delete $pids{$pid};
Branch: 1 branch
BranchTrueFalseTotal
kill 0, $pid or delete $pids{$pid} 2 0 2
188 }
189  
190 3 ▶ $pids{$$}++ if int keys %pids < $count;
Branch: 1 branch
BranchTrueFalseTotal
if int keys %pids < $count 2 1 3
191  
192 3 $self->{'concurrency'}->store( join ',', keys %pids );
193 3 $self->{'concurrency'}->unlock();
194  
195 3 return defined $pids{$$};
196 }
197  
198 =head1 AUTHOR
199  
200 Pawel (bbkr) Pabian, C<< <cpan at bbkr.org> >>
201  
202 Private website: L<http://bbkr.org>
203  
204 Company website: L<http://implix.com>
205  
206 =head1 BUGS
207  
208 Please report any bugs or feature requests to C<bug-ipc-concurrency at rt.cpan.org>, or through
209 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IPC-Concurrency>. I will be notified, and then you'll
210 automatically be notified of progress on your bug as I make changes.
211  
212 B<PID rollover problem>
213  
214 PIDs of processes are stored in containers.
215 Once get_slot() is called it checks how many processes are still active.
216 This is done by sending C<< kill 0, PID >> signals to all processes on ths list.
217 Not responding PIDS are cleared from container and slot is gained if number of PIDs left
218 is smaller than number of concurrent processes required.
219 That makes logic vulnerable to PIDs rollover.
220  
221 =head1 TODO
222  
223 Encrypt/validate container content.
224  
225 =head1 SUPPORT
226  
227 You can find documentation for this module with the perldoc command.
228  
229 perldoc IPC::Concurrency
230  
231  
232 You can also look for information at:
233  
234 =over 4
235  
236 =item * RT: CPAN's request tracker
237  
238 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IPC-Concurrency>
239  
240 =item * AnnoCPAN: Annotated CPAN documentation
241  
242 L<http://annocpan.org/dist/IPC-Concurrency>
243  
244 =item * CPAN Ratings
245  
246 L<http://cpanratings.perl.org/d/IPC-Concurrency>
247  
248 =item * Search CPAN
249  
250 L<http://search.cpan.org/dist/IPC-Concurrency>
251  
252 =back
253  
254  
255 =head1 ACKNOWLEDGEMENTS
256  
257  
258 =head1 COPYRIGHT & LICENSE
259  
260 Copyright 2008 Pawel bbkr Pabian, all rights reserved.
261  
262 This program is free software; you can redistribute it and/or modify it
263 under the same terms as Perl itself.
264  
265  
266 =cut
267  
268 3 1; # End of IPC::Concurrency
↑ Summary