| 1 | package Mojo::IOLoop::ReadWriteProcess::Pool; | ||||||||||||
| 2 | 39 | ▶ | use Mojo::Base 'Mojo::Collection'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 39 | ▶ | use constant MAXIMUM_PROCESSES => $ENV{MOJO_PROCESS_MAXIMUM_PROCESSES} // 100; | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"MOJO_PROCESS_MAXIMUM_PROCESSES"} // 100
MC/DC0%0/1
$ENV{"MOJO_PROCESS_MAXIMUM_PROCESSES"} // 100
$ENV{"MOJO_PROCESS_MAXIMUM_PROCESSES"}
Truth table50%1/2
A
A= $ENV{"MOJO_PROCESS_MAXIMUM_PROCESSES"}
| |||||||||||||
| 4 | 39 | ▶ | use Scalar::Util qw(blessed); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 39 | my %max_proc; | |||||||||||
| 7 | |||||||||||||
| 8 | 57 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 9 | 57 | my $s = shift->SUPER::new(@_); | |||||||||||
| 10 | 57 | $max_proc{$s} = MAXIMUM_PROCESSES; | |||||||||||
| 11 | 57 | $s; | |||||||||||
| 12 | } | ||||||||||||
| 13 | |||||||||||||
| 14 | 5 | ▶ | sub get { @{$_[0]}[$_[1]] } | ||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 15 | 1 | ▶ | sub remove { delete @{$_[0]}[$_[1]] } | ||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 16 | |||||||||||||
| 17 | 188 | ▶ | sub add { | ||||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 18 | 188 | ▶ | return undef unless $_[0]->size < $max_proc{$_[0]}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 154 | my $self = shift; | |||||||||||
| 20 | 154 | ▶ | push @{$self}, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | blessed $_[0] ? $_[0] : Mojo::IOLoop::ReadWriteProcess->new(@_); | ||||||||||||
| 22 | 154 | $self->last; | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | 29 | ▶ | sub maximum_processes { | ||||||||||
|
Subroutine
maximum_processes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 26 | 29 | ▶ | $max_proc{$_[0]} = pop() if $_[1]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 29 | $max_proc{$_[0]}; | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | 20 | ▶ | sub _cmd { | ||||||||||
|
Subroutine
_cmd: called CC=2 SCAR=6.9
| |||||||||||||
| 31 | 20 | my $c = shift; | |||||||||||
| 32 | 20 | my $f = pop; | |||||||||||
| 33 | 20 | my @args = @_; | |||||||||||
| 34 | 20 | my @r; | |||||||||||
| 35 | 20 | ▶ | $c->each(sub { push(@r, +shift()->$f(@args)) }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | 20 | ▶ | wantarray ? @r : $c; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 71 | ▶ | sub AUTOLOAD { | ||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||||
| 40 | 71 | our $AUTOLOAD; | |||||||||||
| 41 | 71 | my $fn = $AUTOLOAD; | |||||||||||
| 42 | 71 | $fn =~ s/.*:://; | |||||||||||
| 43 | 71 | ▶ | return if $fn eq "DESTROY"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 14 | return eval { shift->_cmd(@_, $fn) }; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 39 | 1; | |||||||||||
| 48 | |||||||||||||
| 49 | =encoding utf-8 | ||||||||||||
| 50 | |||||||||||||
| 51 | =head1 NAME | ||||||||||||
| 52 | |||||||||||||
| 53 | Mojo::IOLoop::ReadWriteProcess::Pool - Pool of Mojo::IOLoop::ReadWriteProcess objects. | ||||||||||||
| 54 | |||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||
| 56 | |||||||||||||
| 57 | use Mojo::IOLoop::ReadWriteProcess qw(parallel); | ||||||||||||
| 58 | my $n_proc = 20; | ||||||||||||
| 59 | my $fired; | ||||||||||||
| 60 | |||||||||||||
| 61 | my $p = parallel sub { print "Hello world\n"; } => $n_proc; | ||||||||||||
| 62 | |||||||||||||
| 63 | # Subscribe to all "stop" events in the pool | ||||||||||||
| 64 | $p->once(stop => sub { $fired++; }); | ||||||||||||
| 65 | |||||||||||||
| 66 | # Start all processes belonging to the pool | ||||||||||||
| 67 | $p->start(); | ||||||||||||
| 68 | |||||||||||||
| 69 | # Receive the process output | ||||||||||||
| 70 | $p->each(sub { my $p = shift; $p->getline(); }); | ||||||||||||
| 71 | $p->wait_stop; | ||||||||||||
| 72 | |||||||||||||
| 73 | # Get the last one! (it's a Mojo::Collection!) | ||||||||||||
| 74 | $p->last()->stop(); | ||||||||||||
| 75 | |||||||||||||
| 76 | =head1 METHODS | ||||||||||||
| 77 | |||||||||||||
| 78 | L<Mojo::IOLoop::ReadWriteProcess::Pool> inherits all methods from L<Mojo::Collection> and implements | ||||||||||||
| 79 | the following new ones. | ||||||||||||
| 80 | Note: It proxies all the other methods of L<Mojo::IOLoop::ReadWriteProcess> for the whole process group. | ||||||||||||
| 81 | |||||||||||||
| 82 | =head2 get | ||||||||||||
| 83 | |||||||||||||
| 84 | use Mojo::IOLoop::ReadWriteProcess qw(parallel); | ||||||||||||
| 85 | my $pool = parallel(sub { print "Hello" } => 5); | ||||||||||||
| 86 | $pool->get(4); | ||||||||||||
| 87 | |||||||||||||
| 88 | Get the element specified in the pool (starting from 0). | ||||||||||||
| 89 | |||||||||||||
| 90 | =head2 add | ||||||||||||
| 91 | |||||||||||||
| 92 | use Mojo::IOLoop::ReadWriteProcess qw(pool); | ||||||||||||
| 93 | my $pool = pool(maximum_processes => 2); | ||||||||||||
| 94 | $pool->add(sub { print "Hello 2! " }); | ||||||||||||
| 95 | |||||||||||||
| 96 | Add the element specified in the pool. | ||||||||||||
| 97 | |||||||||||||
| 98 | =head2 remove | ||||||||||||
| 99 | |||||||||||||
| 100 | use Mojo::IOLoop::ReadWriteProcess qw(parallel); | ||||||||||||
| 101 | my $pool = parallel(sub { print "Hello" } => 5); | ||||||||||||
| 102 | $pool->remove(4); | ||||||||||||
| 103 | |||||||||||||
| 104 | Remove the element specified in the pool. | ||||||||||||
| 105 | |||||||||||||
| 106 | =head2 maximum_processes | ||||||||||||
| 107 | |||||||||||||
| 108 | use Mojo::IOLoop::ReadWriteProcess qw(parallel); | ||||||||||||
| 109 | my $pool = parallel(sub { print "Hello" } => 5); | ||||||||||||
| 110 | $pool->maximum_processes(30); | ||||||||||||
| 111 | $pool->add(...); | ||||||||||||
| 112 | |||||||||||||
| 113 | Prevent from adding processes to the pool. If we reach C<maximum_processes> number | ||||||||||||
| 114 | of processes, C<add()> will refuse to add more to the pool. | ||||||||||||
| 115 | |||||||||||||
| 116 | =head1 ENVIRONMENT | ||||||||||||
| 117 | |||||||||||||
| 118 | You can set the MOJO_PROCESS_MAXIMUM_PROCESSES environment variable to specify the | ||||||||||||
| 119 | the maximum number of processes allowed in L<Mojo::IOLoop::ReadWriteProcess> instances. | ||||||||||||
| 120 | |||||||||||||
| 121 | MOJO_PROCESS_MAXIMUM_PROCESSES=10000 | ||||||||||||
| 122 | |||||||||||||
| 123 | =head1 LICENSE | ||||||||||||
| 124 | |||||||||||||
| 125 | Copyright (C) Ettore Di Giacinto. | ||||||||||||
| 126 | |||||||||||||
| 127 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 128 | it under the same terms as Perl itself. | ||||||||||||
| 129 | |||||||||||||
| 130 | =head1 AUTHOR | ||||||||||||
| 131 | |||||||||||||
| 132 | Ettore Di Giacinto E<lt>edigiacinto@suse.comE<gt> | ||||||||||||
| 133 | |||||||||||||
| 134 | =cut | ||||||||||||