1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
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 | |
|
|
5 | THE API IS NOT FINISHED, CONSIDER THIS AN ALPHA RELEASE |
4 | |
6 | |
5 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
6 | |
8 | |
7 | use AnyEvent; |
9 | use AnyEvent; |
8 | use AnyEvent::Fork::Pool; |
10 | use AnyEvent::Fork::Pool; |
… | |
… | |
328 | $proc->[0] |
330 | $proc->[0] |
329 | or --$nidle; |
331 | or --$nidle; |
330 | |
332 | |
331 | Array::Heap::splice_heap_idx @pool, $proc->[1] |
333 | Array::Heap::splice_heap_idx @pool, $proc->[1] |
332 | if defined $proc->[1]; |
334 | if defined $proc->[1]; |
|
|
335 | |
|
|
336 | @$proc = 0; # tell others to leave it be |
333 | }; |
337 | }; |
334 | |
338 | |
335 | $want_start = sub { |
339 | $want_start = sub { |
336 | undef $stop_w; |
340 | undef $stop_w; |
337 | |
341 | |
… | |
… | |
356 | }; |
360 | }; |
357 | |
361 | |
358 | $scheduler = sub { |
362 | $scheduler = sub { |
359 | if (@queue) { |
363 | if (@queue) { |
360 | while (@queue) { |
364 | while (@queue) { |
|
|
365 | @pool or $start_worker->(); |
|
|
366 | |
361 | my $proc = $pool[0]; |
367 | my $proc = $pool[0]; |
362 | |
368 | |
363 | if ($proc->[0] < $load) { |
369 | if ($proc->[0] < $load) { |
364 | # found free worker, increase load |
370 | # found free worker, increase load |
365 | unless ($proc->[0]++) { |
371 | unless ($proc->[0]++) { |
… | |
… | |
383 | or $want_stop->(); |
389 | or $want_stop->(); |
384 | |
390 | |
385 | Array::Heap::adjust_heap_idx @pool, $proc->[1] |
391 | Array::Heap::adjust_heap_idx @pool, $proc->[1] |
386 | if defined $proc->[1]; |
392 | if defined $proc->[1]; |
387 | |
393 | |
|
|
394 | &$ocb; |
|
|
395 | |
388 | $scheduler->(); |
396 | $scheduler->(); |
389 | |
|
|
390 | &$ocb; |
|
|
391 | }); |
397 | }); |
392 | } else { |
398 | } else { |
393 | $want_start->() |
399 | $want_start->() |
394 | unless @pool >= $max; |
400 | unless @pool >= $max; |
395 | |
401 | |
… | |
… | |
513 | having only one worker is best, for example, when you read or write big |
519 | 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. |
520 | files at maixmum speed, as a second worker will increase seek times. |
515 | |
521 | |
516 | =back |
522 | =back |
517 | |
523 | |
|
|
524 | =head1 EXCEPTIONS |
|
|
525 | |
|
|
526 | The same "policy" as with L<AnyEvent::Fork::RPC> applies - exceptins will |
|
|
527 | not be caught, and exceptions in both worker and in callbacks causes |
|
|
528 | undesirable or undefined behaviour. |
|
|
529 | |
518 | =head1 SEE ALSO |
530 | =head1 SEE ALSO |
519 | |
531 | |
520 | L<AnyEvent::Fork>, to create the processes in the first place. |
532 | L<AnyEvent::Fork>, to create the processes in the first place. |
521 | |
533 | |
522 | L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API. |
534 | L<AnyEvent::Fork::RPC>, which implements the RPC protocol and API. |