AnyEvent-Fork-Pool
view release on metacpan or search on metacpan
$want_start->()
unless @pool >= $max;
last;
}
}
} elsif ($shutdown) {
undef $_->[2]
for @pool;
undef $start_w;
undef $start_worker; # frees $destroy_guard reference
$stop_worker->($pool[0])
while $nidle;
}
};
my $shutdown_guard = Guard::guard {
$shutdown = 1;
$scheduler->();
};
$start_worker->()
while @pool < $idle;
sub {
$shutdown_guard if 0; # keep it alive
$start_worker->()
unless @pool;
push @queue, [@_];
$scheduler->();
}
}
=item $pool->(..., $cb->(...))
Call the RPC function of a worker with the given arguments, and when the
worker is done, call the C<$cb> with the results, just like calling the
RPC object durectly - see the L<AnyEvent::Fork::RPC> documentation for
details on the RPC API.
If there is no free worker, the call will be queued until a worker becomes
available.
Note that there can be considerable time between calling this method and
the call actually being executed. During this time, the parameters passed
to this function are effectively read-only - modifying them after the call
and before the callback is invoked causes undefined behaviour.
=cut
=item $cpus = AnyEvent::Fork::Pool::ncpu [$default_cpus]
=item ($cpus, $eus) = AnyEvent::Fork::Pool::ncpu [$default_cpus]
Tries to detect the number of CPUs (C<$cpus> often called CPU cores
nowadays) and execution units (C<$eus>) which include e.g. extra
hyperthreaded units). When C<$cpus> cannot be determined reliably,
C<$default_cpus> is returned for both values, or C<1> if it is missing.
For normal CPU bound uses, it is wise to have as many worker processes
as CPUs in the system (C<$cpus>), if nothing else uses the CPU. Using
hyperthreading is usually detrimental to performance, but in those rare
cases where that really helps it might be beneficial to use more workers
(C<$eus>).
Currently, F</proc/cpuinfo> is parsed on GNU/Linux systems for both
C<$cpus> and C<$eus>, and on {Free,Net,Open}BSD, F<sysctl -n hw.ncpu> is
used for C<$cpus>.
Example: create a worker pool with as many workers as CPU cores, or C<2>,
if the actual number could not be determined.
$fork->AnyEvent::Fork::Pool::run ("myworker::function",
max => (scalar AnyEvent::Fork::Pool::ncpu 2),
);
=cut
BEGIN {
if ($^O eq "linux") {
*ncpu = sub(;$) {
my ($cpus, $eus);
if (open my $fh, "<", "/proc/cpuinfo") {
my %id;
while (<$fh>) {
if (/^core id\s*:\s*(\d+)/) {
++$eus;
undef $id{$1};
}
}
$cpus = scalar keys %id;
} else {
$cpus = $eus = @_ ? shift : 1;
}
wantarray ? ($cpus, $eus) : $cpus
};
} elsif ($^O eq "freebsd" || $^O eq "netbsd" || $^O eq "openbsd") {
*ncpu = sub(;$) {
my $cpus = qx<sysctl -n hw.ncpu> * 1
|| (@_ ? shift : 1);
wantarray ? ($cpus, $cpus) : $cpus
};
} else {
*ncpu = sub(;$) {
my $cpus = @_ ? shift : 1;
wantarray ? ($cpus, $cpus) : $cpus
};
}
}
=back
=head1 CHILD USAGE
In addition to the L<AnyEvent::Fork::RPC> API, this module implements one
more child-side function:
=over 4
( run in 1.652 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )