… | |
… | |
3 | AnyEvent::Fork::Pool - simple process pool manager on top of AnyEvent::Fork |
3 | AnyEvent::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 | |
46 | This module uses processes created via L<AnyEvent::Fork> and the RPC |
46 | This module uses processes created via L<AnyEvent::Fork> (or |
47 | protocol implement in L<AnyEvent::Fork::RPC> to create a load-balanced |
47 | L<AnyEvent::Fork::Remote>) and the RPC protocol implement in |
48 | pool of processes that handles jobs. |
48 | L<AnyEvent::Fork::RPC> to create a load-balanced pool of processes that |
|
|
49 | handles jobs. |
49 | |
50 | |
50 | Understanding of L<AnyEvent::Fork> is helpful but not critical to be able |
51 | Understanding of L<AnyEvent::Fork> is helpful but not critical to be able |
51 | to use this module, but a thorough understanding of L<AnyEvent::Fork::RPC> |
52 | to use this module, but a thorough understanding of L<AnyEvent::Fork::RPC> |
52 | is, as it defines the actual API that needs to be implemented in the |
53 | is, as it defines the actual API that needs to be implemented in the |
53 | worker processes. |
54 | worker processes. |
54 | |
|
|
55 | =head1 EXAMPLES |
|
|
56 | |
55 | |
57 | =head1 PARENT USAGE |
56 | =head1 PARENT USAGE |
58 | |
57 | |
59 | To create a pool, you first have to create a L<AnyEvent::Fork> object - |
58 | To create a pool, you first have to create a L<AnyEvent::Fork> object - |
60 | this object becomes your template process. Whenever a new worker process |
59 | this object becomes your template process. Whenever a new worker process |
… | |
… | |
87 | |
86 | |
88 | use Guard (); |
87 | use Guard (); |
89 | use Array::Heap (); |
88 | use Array::Heap (); |
90 | |
89 | |
91 | use AnyEvent; |
90 | use AnyEvent; |
92 | use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
|
|
93 | use AnyEvent::Fork::RPC; |
91 | use 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. |
98 | my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; |
96 | my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; |
99 | my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; |
97 | my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; |
100 | |
98 | |
101 | our $VERSION = 0.1; |
99 | our $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 | |
105 | The traditional way to call the pool creation function. But it is way |
103 | The traditional way to call the pool creation function. But it is way |
106 | cooler to call it in the following way: |
104 | cooler to call it in the following way: |
… | |
… | |
328 | $proc->[0] |
326 | $proc->[0] |
329 | or --$nidle; |
327 | or --$nidle; |
330 | |
328 | |
331 | Array::Heap::splice_heap_idx @pool, $proc->[1] |
329 | Array::Heap::splice_heap_idx @pool, $proc->[1] |
332 | if defined $proc->[1]; |
330 | if defined $proc->[1]; |
|
|
331 | |
|
|
332 | @$proc = 0; # tell others to leave it be |
333 | }; |
333 | }; |
334 | |
334 | |
335 | $want_start = sub { |
335 | $want_start = sub { |
336 | undef $stop_w; |
336 | undef $stop_w; |
337 | |
337 | |
… | |
… | |
356 | }; |
356 | }; |
357 | |
357 | |
358 | $scheduler = sub { |
358 | $scheduler = sub { |
359 | if (@queue) { |
359 | if (@queue) { |
360 | while (@queue) { |
360 | while (@queue) { |
|
|
361 | @pool or $start_worker->(); |
|
|
362 | |
361 | my $proc = $pool[0]; |
363 | my $proc = $pool[0]; |
362 | |
364 | |
363 | if ($proc->[0] < $load) { |
365 | if ($proc->[0] < $load) { |
364 | # found free worker, increase load |
366 | # found free worker, increase load |
365 | unless ($proc->[0]++) { |
367 | unless ($proc->[0]++) { |
… | |
… | |
383 | or $want_stop->(); |
385 | or $want_stop->(); |
384 | |
386 | |
385 | Array::Heap::adjust_heap_idx @pool, $proc->[1] |
387 | Array::Heap::adjust_heap_idx @pool, $proc->[1] |
386 | if defined $proc->[1]; |
388 | if defined $proc->[1]; |
387 | |
389 | |
|
|
390 | &$ocb; |
|
|
391 | |
388 | $scheduler->(); |
392 | $scheduler->(); |
389 | |
|
|
390 | &$ocb; |
|
|
391 | }); |
393 | }); |
392 | } else { |
394 | } else { |
393 | $want_start->() |
395 | $want_start->() |
394 | unless @pool >= $max; |
396 | unless @pool >= $max; |
395 | |
397 | |
… | |
… | |
440 | to this function are effectively read-only - modifying them after the call |
442 | to this function are effectively read-only - modifying them after the call |
441 | and before the callback is invoked causes undefined behaviour. |
443 | and before the callback is invoked causes undefined behaviour. |
442 | |
444 | |
443 | =cut |
445 | =cut |
444 | |
446 | |
|
|
447 | =item $cpus = AnyEvent::Fork::Pool::ncpu [$default_cpus] |
|
|
448 | |
|
|
449 | =item ($cpus, $eus) = AnyEvent::Fork::Pool::ncpu [$default_cpus] |
|
|
450 | |
|
|
451 | Tries to detect the number of CPUs (C<$cpus> often called CPU cores |
|
|
452 | nowadays) and execution units (C<$eus>) which include e.g. extra |
|
|
453 | hyperthreaded units). When C<$cpus> cannot be determined reliably, |
|
|
454 | C<$default_cpus> is returned for both values, or C<1> if it is missing. |
|
|
455 | |
|
|
456 | For normal CPU bound uses, it is wise to have as many worker processes |
|
|
457 | as CPUs in the system (C<$cpus>), if nothing else uses the CPU. Using |
|
|
458 | hyperthreading is usually detrimental to performance, but in those rare |
|
|
459 | cases where that really helps it might be beneficial to use more workers |
|
|
460 | (C<$eus>). |
|
|
461 | |
|
|
462 | Currently, F</proc/cpuinfo> is parsed on GNU/Linux systems for both |
|
|
463 | C<$cpus> and C<$eus>, and on {Free,Net,Open}BSD, F<sysctl -n hw.ncpu> is |
|
|
464 | used for C<$cpus>. |
|
|
465 | |
|
|
466 | Example: create a worker pool with as many workers as CPU cores, or C<2>, |
|
|
467 | if 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 | |
|
|
475 | BEGIN { |
|
|
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 | |
445 | =back |
510 | =back |
446 | |
511 | |
447 | =head1 CHILD USAGE |
512 | =head1 CHILD USAGE |
448 | |
513 | |
449 | In addition to the L<AnyEvent::Fork::RPC> API, this module implements one |
514 | In addition to the L<AnyEvent::Fork::RPC> API, this module implements one |
… | |
… | |
453 | |
518 | |
454 | =item AnyEvent::Fork::Pool::retire () |
519 | =item AnyEvent::Fork::Pool::retire () |
455 | |
520 | |
456 | This function sends an event to the parent process to request retirement: |
521 | This function sends an event to the parent process to request retirement: |
457 | the worker is removed from the pool and no new jobs will be sent to it, |
522 | the worker is removed from the pool and no new jobs will be sent to it, |
458 | but it has to handle the jobs that are already queued. |
523 | but it still has to handle the jobs that are already queued. |
459 | |
524 | |
460 | The parentheses are part of the syntax: the function usually isn't defined |
525 | The parentheses are part of the syntax: the function usually isn't defined |
461 | when you compile your code (because that happens I<before> handing the |
526 | when you compile your code (because that happens I<before> handing the |
462 | template process over to C<AnyEvent::Fork::Pool::run>, so you need the |
527 | template process over to C<AnyEvent::Fork::Pool::run>, so you need the |
463 | empty parentheses to tell Perl that the function is indeed a function. |
528 | empty parentheses to tell Perl that the function is indeed a function. |
464 | |
529 | |
465 | Retiring a worker can be useful to gracefully shut it down when the worker |
530 | Retiring a worker can be useful to gracefully shut it down when the worker |
466 | deems this useful. For example, after executing a job, one could check |
531 | deems this useful. For example, after executing a job, it could check the |
467 | the process size or the number of jobs handled so far, and if either is |
532 | process size or the number of jobs handled so far, and if either is too |
468 | too high, the worker could ask to get retired, to avoid memory leaks to |
533 | high, the worker could request to be retired, to avoid memory leaks to |
469 | accumulate. |
534 | accumulate. |
470 | |
535 | |
|
|
536 | Example: retire a worker after it has handled roughly 100 requests. It |
|
|
537 | doesn't matter whether you retire at the beginning or end of your request, |
|
|
538 | as the worker will continue to handle some outstanding requests. Likewise, |
|
|
539 | it'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 | |
471 | =back |
551 | =back |
472 | |
552 | |
473 | =head1 POOL PARAMETERS RECIPES |
553 | =head1 POOL PARAMETERS RECIPES |
474 | |
554 | |
475 | This section describes some recipes for pool paramaters. These are mostly |
555 | This section describes some recipes for pool parameters. These are mostly |
476 | meant for the synchronous RPC backend, as the asynchronous RPC backend |
556 | meant for the synchronous RPC backend, as the asynchronous RPC backend |
477 | changes the rules considerably, making workers themselves responsible for |
557 | changes the rules considerably, making workers themselves responsible for |
478 | their scheduling. |
558 | their scheduling. |
479 | |
559 | |
480 | =over 4 |
560 | =over 4 |
… | |
… | |
509 | =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 |
510 | |
590 | |
511 | When your jobs are I/O bound, using more workers usually boils down to |
591 | When your jobs are I/O bound, using more workers usually boils down to |
512 | higher throughput, depending very much on your actual workload - sometimes |
592 | higher throughput, depending very much on your actual workload - sometimes |
513 | having only one worker is best, for example, when you read or write big |
593 | having only one worker is best, for example, when you read or write big |
514 | files at maixmum speed, as a second worker will increase seek times. |
594 | files at maximum speed, as a second worker will increase seek times. |
515 | |
595 | |
516 | =back |
596 | =back |
517 | |
597 | |
|
|
598 | =head1 EXCEPTIONS |
|
|
599 | |
|
|
600 | The same "policy" as with L<AnyEvent::Fork::RPC> applies - exceptions |
|
|
601 | will not be caught, and exceptions in both worker and in callbacks causes |
|
|
602 | undesirable or undefined behaviour. |
|
|
603 | |
518 | =head1 SEE ALSO |
604 | =head1 SEE ALSO |
519 | |
605 | |
520 | L<AnyEvent::Fork>, to create the processes in the first place. |
606 | L<AnyEvent::Fork>, to create the processes in the first place. |
|
|
607 | |
|
|
608 | L<AnyEvent::Fork::Remote>, likewise, but helpful for remote processes. |
521 | |
609 | |
522 | L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API. |
610 | L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API. |
523 | |
611 | |
524 | =head1 AUTHOR AND CONTACT INFORMATION |
612 | =head1 AUTHOR AND CONTACT INFORMATION |
525 | |
613 | |