ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-Pool/Pool.pm
(Generate patch)

Comparing AnyEvent-Fork-Pool/Pool.pm (file contents):
Revision 1.8 by root, Sun Apr 21 12:28:34 2013 UTC vs.
Revision 1.13 by root, Sun Apr 28 14:27:31 2013 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::Fork::Pool - simple process pool manager on top of AnyEvent::Fork 3AnyEvent::Fork::Pool - simple process pool manager on top of AnyEvent::Fork
4
5THE API IS NOT FINISHED, CONSIDER THIS AN ALPHA RELEASE
6 4
7=head1 SYNOPSIS 5=head1 SYNOPSIS
8 6
9 use AnyEvent; 7 use AnyEvent;
10 use AnyEvent::Fork::Pool; 8 use AnyEvent::Fork::Pool;
43 41
44 $finish->recv; 42 $finish->recv;
45 43
46=head1 DESCRIPTION 44=head1 DESCRIPTION
47 45
48This module uses processes created via L<AnyEvent::Fork> and the RPC 46This module uses processes created via L<AnyEvent::Fork> (or
49protocol implement in L<AnyEvent::Fork::RPC> to create a load-balanced 47L<AnyEvent::Fork::Remote>) and the RPC protocol implement in
50pool of processes that handles jobs. 48L<AnyEvent::Fork::RPC> to create a load-balanced pool of processes that
49handles jobs.
51 50
52Understanding of L<AnyEvent::Fork> is helpful but not critical to be able 51Understanding of L<AnyEvent::Fork> is helpful but not critical to be able
53to use this module, but a thorough understanding of L<AnyEvent::Fork::RPC> 52to use this module, but a thorough understanding of L<AnyEvent::Fork::RPC>
54is, as it defines the actual API that needs to be implemented in the 53is, as it defines the actual API that needs to be implemented in the
55worker processes. 54worker processes.
56
57=head1 EXAMPLES
58 55
59=head1 PARENT USAGE 56=head1 PARENT USAGE
60 57
61To create a pool, you first have to create a L<AnyEvent::Fork> object - 58To create a pool, you first have to create a L<AnyEvent::Fork> object -
62this object becomes your template process. Whenever a new worker process 59this object becomes your template process. Whenever a new worker process
89 86
90use Guard (); 87use Guard ();
91use Array::Heap (); 88use Array::Heap ();
92 89
93use AnyEvent; 90use AnyEvent;
91# explicit version on next line, as some cpan-testers test with the 0.1 version,
92# ignoring dependencies, and this line will at least give a clear indication of that.
94use AnyEvent::Fork; # we don't actually depend on it, this is for convenience 93use AnyEvent::Fork 0.6; # we don't actually depend on it, this is for convenience
95use AnyEvent::Fork::RPC; 94use AnyEvent::Fork::RPC;
96 95
97# these are used for the first and last argument of events 96# these are used for the first and last argument of events
98# in the hope of not colliding. yes, I don't like it either, 97# in the hope of not colliding. yes, I don't like it either,
99# but didn't come up with an obviously better alternative. 98# but didn't come up with an obviously better alternative.
100my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; 99my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P';
101my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; 100my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*';
102 101
103our $VERSION = 0.1; 102our $VERSION = 1.1;
104 103
105=item my $pool = AnyEvent::Fork::Pool::run $fork, $function, [key => value...] 104=item my $pool = AnyEvent::Fork::Pool::run $fork, $function, [key => value...]
106 105
107The traditional way to call the pool creation function. But it is way 106The traditional way to call the pool creation function. But it is way
108cooler to call it in the following way: 107cooler to call it in the following way:
446to this function are effectively read-only - modifying them after the call 445to this function are effectively read-only - modifying them after the call
447and before the callback is invoked causes undefined behaviour. 446and before the callback is invoked causes undefined behaviour.
448 447
449=cut 448=cut
450 449
450=item $cpus = AnyEvent::Fork::Pool::ncpu [$default_cpus]
451
452=item ($cpus, $eus) = AnyEvent::Fork::Pool::ncpu [$default_cpus]
453
454Tries to detect the number of CPUs (C<$cpus> often called cpu cores
455nowadays) and execution units (C<$eus>) which include e.g. extra
456hyperthreaded units). When C<$cpus> cannot be determined reliably,
457C<$default_cpus> is returned for both values, or C<1> if it is missing.
458
459For normal CPU bound uses, it is wise to have as many worker processes
460as CPUs in the system (C<$cpus>), if nothing else uses the CPU. Using
461hyperthreading is usually detrimental to performance, but in those rare
462cases where that really helps it might be beneficial to use more workers
463(C<$eus>).
464
465Currently, F</proc/cpuinfo> is parsed on GNU/Linux systems for both
466C<$cpus> and C<$eu>, and on {Free,Net,Open}BSD, F<sysctl -n hw.ncpu> is
467used for C<$cpus>.
468
469Example: create a worker pool with as many workers as cpu cores, or C<2>,
470if the actual number could not be determined.
471
472 $fork->AnyEvent::Fork::Pool::run ("myworker::function",
473 max => (scalar AnyEvent::Fork::Pool::ncpu 2),
474 );
475
476=cut
477
478BEGIN {
479 if ($^O eq "linux") {
480 *ncpu = sub(;$) {
481 my ($cpus, $eus);
482
483 if (open my $fh, "<", "/proc/cpuinfo") {
484 my %id;
485
486 while (<$fh>) {
487 if (/^core id\s*:\s*(\d+)/) {
488 ++$eus;
489 undef $id{$1};
490 }
491 }
492
493 $cpus = scalar keys %id;
494 } else {
495 $cpus = $eus = @_ ? shift : 1;
496 }
497 wantarray ? ($cpus, $eus) : $cpus
498 };
499 } elsif ($^O eq "freebsd" || $^O eq "netbsd" || $^O eq "openbsd") {
500 *ncpu = sub(;$) {
501 my $cpus = qx<sysctl -n hw.ncpu> * 1
502 || (@_ ? shift : 1);
503 wantarray ? ($cpus, $cpus) : $cpus
504 };
505 } else {
506 *ncpu = sub(;$) {
507 my $cpus = @_ ? shift : 1;
508 wantarray ? ($cpus, $cpus) : $cpus
509 };
510 }
511}
512
451=back 513=back
452 514
453=head1 CHILD USAGE 515=head1 CHILD USAGE
454 516
455In addition to the L<AnyEvent::Fork::RPC> API, this module implements one 517In addition to the L<AnyEvent::Fork::RPC> API, this module implements one
472deems this useful. For example, after executing a job, one could check 534deems this useful. For example, after executing a job, one could check
473the process size or the number of jobs handled so far, and if either is 535the process size or the number of jobs handled so far, and if either is
474too high, the worker could ask to get retired, to avoid memory leaks to 536too high, the worker could ask to get retired, to avoid memory leaks to
475accumulate. 537accumulate.
476 538
539Example: retire a worker after it has handled roughly 100 requests.
540
541 my $count = 0;
542
543 sub my::worker {
544
545 ++$count == 100
546 and AnyEvent::Fork::Pool::retire ();
547
548 ... normal code goes here
549 }
550
477=back 551=back
478 552
479=head1 POOL PARAMETERS RECIPES 553=head1 POOL PARAMETERS RECIPES
480 554
481This section describes some recipes for pool paramaters. These are mostly 555This section describes some recipes for pool paramaters. These are mostly
529 603
530=head1 SEE ALSO 604=head1 SEE ALSO
531 605
532L<AnyEvent::Fork>, to create the processes in the first place. 606L<AnyEvent::Fork>, to create the processes in the first place.
533 607
608L<AnyEvent::Fork::Remote>, likewise, but helpful for remote processes.
609
534L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API. 610L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API.
535 611
536=head1 AUTHOR AND CONTACT INFORMATION 612=head1 AUTHOR AND CONTACT INFORMATION
537 613
538 Marc Lehmann <schmorp@schmorp.de> 614 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines