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.7 by root, Sun Apr 21 12:03:02 2013 UTC vs.
Revision 1.14 by root, Sun Oct 26 16:22:38 2014 UTC

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 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent; 7 use AnyEvent;
8 use AnyEvent::Fork;
8 use AnyEvent::Fork::Pool; 9 use AnyEvent::Fork::Pool;
9 # use AnyEvent::Fork is not needed
10 10
11 # all possible parameters shown, with default values 11 # all possible parameters shown, with default values
12 my $pool = AnyEvent::Fork 12 my $pool = AnyEvent::Fork
13 ->new 13 ->new
14 ->require ("MyWorker") 14 ->require ("MyWorker")
41 41
42 $finish->recv; 42 $finish->recv;
43 43
44=head1 DESCRIPTION 44=head1 DESCRIPTION
45 45
46This module uses processes created via L<AnyEvent::Fork> and the RPC 46This module uses processes created via L<AnyEvent::Fork> (or
47protocol implement in L<AnyEvent::Fork::RPC> to create a load-balanced 47L<AnyEvent::Fork::Remote>) and the RPC protocol implement in
48pool of processes that handles jobs. 48L<AnyEvent::Fork::RPC> to create a load-balanced pool of processes that
49handles jobs.
49 50
50Understanding 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
51to 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>
52is, 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
53worker processes. 54worker processes.
54
55=head1 EXAMPLES
56 55
57=head1 PARENT USAGE 56=head1 PARENT USAGE
58 57
59To 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 -
60this object becomes your template process. Whenever a new worker process 59this object becomes your template process. Whenever a new worker process
87 86
88use Guard (); 87use Guard ();
89use Array::Heap (); 88use Array::Heap ();
90 89
91use AnyEvent; 90use AnyEvent;
92use AnyEvent::Fork; # we don't actually depend on it, this is for convenience
93use AnyEvent::Fork::RPC; 91use AnyEvent::Fork::RPC;
94 92
95# these are used for the first and last argument of events 93# these are used for the first and last argument of events
96# in the hope of not colliding. yes, I don't like it either, 94# in the hope of not colliding. yes, I don't like it either,
97# but didn't come up with an obviously better alternative. 95# but didn't come up with an obviously better alternative.
98my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; 96my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P';
99my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; 97my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*';
100 98
101our $VERSION = 0.1; 99our $VERSION = 1.1;
102 100
103=item my $pool = AnyEvent::Fork::Pool::run $fork, $function, [key => value...] 101=item my $pool = AnyEvent::Fork::Pool::run $fork, $function, [key => value...]
104 102
105The traditional way to call the pool creation function. But it is way 103The traditional way to call the pool creation function. But it is way
106cooler to call it in the following way: 104cooler to call it in the following way:
444to this function are effectively read-only - modifying them after the call 442to this function are effectively read-only - modifying them after the call
445and before the callback is invoked causes undefined behaviour. 443and before the callback is invoked causes undefined behaviour.
446 444
447=cut 445=cut
448 446
447=item $cpus = AnyEvent::Fork::Pool::ncpu [$default_cpus]
448
449=item ($cpus, $eus) = AnyEvent::Fork::Pool::ncpu [$default_cpus]
450
451Tries to detect the number of CPUs (C<$cpus> often called CPU cores
452nowadays) and execution units (C<$eus>) which include e.g. extra
453hyperthreaded units). When C<$cpus> cannot be determined reliably,
454C<$default_cpus> is returned for both values, or C<1> if it is missing.
455
456For normal CPU bound uses, it is wise to have as many worker processes
457as CPUs in the system (C<$cpus>), if nothing else uses the CPU. Using
458hyperthreading is usually detrimental to performance, but in those rare
459cases where that really helps it might be beneficial to use more workers
460(C<$eus>).
461
462Currently, F</proc/cpuinfo> is parsed on GNU/Linux systems for both
463C<$cpus> and C<$eus>, and on {Free,Net,Open}BSD, F<sysctl -n hw.ncpu> is
464used for C<$cpus>.
465
466Example: create a worker pool with as many workers as CPU cores, or C<2>,
467if the actual number could not be determined.
468
469 $fork->AnyEvent::Fork::Pool::run ("myworker::function",
470 max => (scalar AnyEvent::Fork::Pool::ncpu 2),
471 );
472
473=cut
474
475BEGIN {
476 if ($^O eq "linux") {
477 *ncpu = sub(;$) {
478 my ($cpus, $eus);
479
480 if (open my $fh, "<", "/proc/cpuinfo") {
481 my %id;
482
483 while (<$fh>) {
484 if (/^core id\s*:\s*(\d+)/) {
485 ++$eus;
486 undef $id{$1};
487 }
488 }
489
490 $cpus = scalar keys %id;
491 } else {
492 $cpus = $eus = @_ ? shift : 1;
493 }
494 wantarray ? ($cpus, $eus) : $cpus
495 };
496 } elsif ($^O eq "freebsd" || $^O eq "netbsd" || $^O eq "openbsd") {
497 *ncpu = sub(;$) {
498 my $cpus = qx<sysctl -n hw.ncpu> * 1
499 || (@_ ? shift : 1);
500 wantarray ? ($cpus, $cpus) : $cpus
501 };
502 } else {
503 *ncpu = sub(;$) {
504 my $cpus = @_ ? shift : 1;
505 wantarray ? ($cpus, $cpus) : $cpus
506 };
507 }
508}
509
449=back 510=back
450 511
451=head1 CHILD USAGE 512=head1 CHILD USAGE
452 513
453In addition to the L<AnyEvent::Fork::RPC> API, this module implements one 514In addition to the L<AnyEvent::Fork::RPC> API, this module implements one
457 518
458=item AnyEvent::Fork::Pool::retire () 519=item AnyEvent::Fork::Pool::retire ()
459 520
460This function sends an event to the parent process to request retirement: 521This function sends an event to the parent process to request retirement:
461the worker is removed from the pool and no new jobs will be sent to it, 522the worker is removed from the pool and no new jobs will be sent to it,
462but it has to handle the jobs that are already queued. 523but it still has to handle the jobs that are already queued.
463 524
464The parentheses are part of the syntax: the function usually isn't defined 525The parentheses are part of the syntax: the function usually isn't defined
465when you compile your code (because that happens I<before> handing the 526when you compile your code (because that happens I<before> handing the
466template process over to C<AnyEvent::Fork::Pool::run>, so you need the 527template process over to C<AnyEvent::Fork::Pool::run>, so you need the
467empty parentheses to tell Perl that the function is indeed a function. 528empty parentheses to tell Perl that the function is indeed a function.
468 529
469Retiring a worker can be useful to gracefully shut it down when the worker 530Retiring a worker can be useful to gracefully shut it down when the worker
470deems this useful. For example, after executing a job, one could check 531deems this useful. For example, after executing a job, it could check the
471the process size or the number of jobs handled so far, and if either is 532process size or the number of jobs handled so far, and if either is too
472too high, the worker could ask to get retired, to avoid memory leaks to 533high, the worker could request to be retired, to avoid memory leaks to
473accumulate. 534accumulate.
474 535
536Example: retire a worker after it has handled roughly 100 requests. It
537doesn't matter whether you retire at the beginning or end of your request,
538as the worker will continue to handle some outstanding requests. Likewise,
539it's ok to call retire multiple times.
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
475=back 551=back
476 552
477=head1 POOL PARAMETERS RECIPES 553=head1 POOL PARAMETERS RECIPES
478 554
479This section describes some recipes for pool paramaters. These are mostly 555This section describes some recipes for pool parameters. These are mostly
480meant for the synchronous RPC backend, as the asynchronous RPC backend 556meant for the synchronous RPC backend, as the asynchronous RPC backend
481changes the rules considerably, making workers themselves responsible for 557changes the rules considerably, making workers themselves responsible for
482their scheduling. 558their scheduling.
483 559
484=over 4 560=over 4
513=item high throughput, I/O bound jobs - set load >= 2, max = 1, or very high 589=item high throughput, I/O bound jobs - set load >= 2, max = 1, or very high
514 590
515When your jobs are I/O bound, using more workers usually boils down to 591When your jobs are I/O bound, using more workers usually boils down to
516higher throughput, depending very much on your actual workload - sometimes 592higher throughput, depending very much on your actual workload - sometimes
517having only one worker is best, for example, when you read or write big 593having only one worker is best, for example, when you read or write big
518files at maixmum speed, as a second worker will increase seek times. 594files at maximum speed, as a second worker will increase seek times.
519 595
520=back 596=back
521 597
522=head1 EXCEPTIONS 598=head1 EXCEPTIONS
523 599
524The same "policy" as with L<AnyEvent::Fork::RPC> applies - exceptins will 600The same "policy" as with L<AnyEvent::Fork::RPC> applies - exceptions
525not be caught, and exceptions in both worker and in callbacks causes 601will not be caught, and exceptions in both worker and in callbacks causes
526undesirable or undefined behaviour. 602undesirable or undefined behaviour.
527 603
528=head1 SEE ALSO 604=head1 SEE ALSO
529 605
530L<AnyEvent::Fork>, to create the processes in the first place. 606L<AnyEvent::Fork>, to create the processes in the first place.
607
608L<AnyEvent::Fork::Remote>, likewise, but helpful for remote processes.
531 609
532L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API. 610L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API.
533 611
534=head1 AUTHOR AND CONTACT INFORMATION 612=head1 AUTHOR AND CONTACT INFORMATION
535 613

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines