… | |
… | |
34 | This module only creates processes and lets you pass file handles and |
34 | This module only creates processes and lets you pass file handles and |
35 | strings to it, and run perl code. It does not implement any kind of RPC - |
35 | strings to it, and run perl code. It does not implement any kind of RPC - |
36 | there is no back channel from the process back to you, and there is no RPC |
36 | there is no back channel from the process back to you, and there is no RPC |
37 | or message passing going on. |
37 | or message passing going on. |
38 | |
38 | |
39 | If you need some form of RPC, you can either implement it yourself |
39 | If you need some form of RPC, you could use the L<AnyEvent::Fork::RPC> |
40 | in whatever way you like, use some message-passing module such |
40 | companion module, which adds simple RPC/job queueing to a process created |
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
41 | by this module. |
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
42 | |
43 | and so on. |
43 | Or you can implement it yourself in whatever way you like, use some |
|
|
44 | message-passing module such as L<AnyEvent::MP>, some pipe such as |
|
|
45 | L<AnyEvent::ZeroMQ>, use L<AnyEvent::Handle> on both sides to send |
|
|
46 | e.g. JSON or Storable messages, and so on. |
44 | |
47 | |
45 | =head2 COMPARISON TO OTHER MODULES |
48 | =head2 COMPARISON TO OTHER MODULES |
46 | |
49 | |
47 | There is an abundance of modules on CPAN that do "something fork", such as |
50 | There is an abundance of modules on CPAN that do "something fork", such as |
48 | L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker> |
51 | L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker> |
… | |
… | |
372 | |
375 | |
373 | use IO::FDPass; |
376 | use IO::FDPass; |
374 | |
377 | |
375 | our $VERSION = 0.6; |
378 | our $VERSION = 0.6; |
376 | |
379 | |
377 | =over 4 |
|
|
378 | |
|
|
379 | =back |
|
|
380 | |
|
|
381 | =cut |
|
|
382 | |
|
|
383 | # the early fork template process |
380 | # the early fork template process |
384 | our $EARLY; |
381 | our $EARLY; |
385 | |
382 | |
386 | # the empty template process |
383 | # the empty template process |
387 | our $TEMPLATE; |
384 | our $TEMPLATE; |
|
|
385 | |
|
|
386 | sub QUEUE() { 0 } |
|
|
387 | sub FH() { 1 } |
|
|
388 | sub WW() { 2 } |
|
|
389 | sub PID() { 3 } |
|
|
390 | sub CB() { 4 } |
|
|
391 | |
|
|
392 | sub _new { |
|
|
393 | my ($self, $fh, $pid) = @_; |
|
|
394 | |
|
|
395 | AnyEvent::Util::fh_nonblocking $fh, 1; |
|
|
396 | |
|
|
397 | $self = bless [ |
|
|
398 | [], # write queue - strings or fd's |
|
|
399 | $fh, |
|
|
400 | undef, # AE watcher |
|
|
401 | $pid, |
|
|
402 | ], $self; |
|
|
403 | |
|
|
404 | $self |
|
|
405 | } |
388 | |
406 | |
389 | sub _cmd { |
407 | sub _cmd { |
390 | my $self = shift; |
408 | my $self = shift; |
391 | |
409 | |
392 | # ideally, we would want to use "a (w/a)*" as format string, but perl |
410 | # ideally, we would want to use "a (w/a)*" as format string, but perl |
393 | # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack |
411 | # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack |
394 | # it. |
412 | # it. |
395 | push @{ $self->[2] }, pack "a L/a*", $_[0], $_[1]; |
413 | push @{ $self->[QUEUE] }, pack "a L/a*", $_[0], $_[1]; |
396 | |
414 | |
397 | $self->[3] ||= AE::io $self->[1], 1, sub { |
415 | $self->[WW] ||= AE::io $self->[FH], 1, sub { |
398 | do { |
416 | do { |
399 | # send the next "thing" in the queue - either a reference to an fh, |
417 | # send the next "thing" in the queue - either a reference to an fh, |
400 | # or a plain string. |
418 | # or a plain string. |
401 | |
419 | |
402 | if (ref $self->[2][0]) { |
420 | if (ref $self->[QUEUE][0]) { |
403 | # send fh |
421 | # send fh |
404 | unless (IO::FDPass::send fileno $self->[1], fileno ${ $self->[2][0] }) { |
422 | unless (IO::FDPass::send fileno $self->[FH], fileno ${ $self->[QUEUE][0] }) { |
405 | return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; |
423 | return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; |
406 | undef $self->[3]; |
424 | undef $self->[WW]; |
407 | die "AnyEvent::Fork: file descriptor send failure: $!"; |
425 | die "AnyEvent::Fork: file descriptor send failure: $!"; |
408 | } |
426 | } |
409 | |
427 | |
410 | shift @{ $self->[2] }; |
428 | shift @{ $self->[QUEUE] }; |
411 | |
429 | |
412 | } else { |
430 | } else { |
413 | # send string |
431 | # send string |
414 | my $len = syswrite $self->[1], $self->[2][0]; |
432 | my $len = syswrite $self->[FH], $self->[QUEUE][0]; |
415 | |
433 | |
416 | unless ($len) { |
434 | unless ($len) { |
417 | return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; |
435 | return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; |
418 | undef $self->[3]; |
436 | undef $self->[3]; |
419 | die "AnyEvent::Fork: command write failure: $!"; |
437 | die "AnyEvent::Fork: command write failure: $!"; |
420 | } |
438 | } |
421 | |
439 | |
422 | substr $self->[2][0], 0, $len, ""; |
440 | substr $self->[QUEUE][0], 0, $len, ""; |
423 | shift @{ $self->[2] } unless length $self->[2][0]; |
441 | shift @{ $self->[QUEUE] } unless length $self->[QUEUE][0]; |
424 | } |
442 | } |
425 | } while @{ $self->[2] }; |
443 | } while @{ $self->[QUEUE] }; |
426 | |
444 | |
427 | # everything written |
445 | # everything written |
428 | undef $self->[3]; |
446 | undef $self->[WW]; |
429 | |
447 | |
430 | # invoke run callback, if any |
448 | # invoke run callback, if any |
431 | $self->[4]->($self->[1]) if $self->[4]; |
449 | $self->[CB]->($self->[FH]) if $self->[CB]; |
432 | }; |
450 | }; |
433 | |
451 | |
434 | () # make sure we don't leak the watcher |
452 | () # make sure we don't leak the watcher |
435 | } |
|
|
436 | |
|
|
437 | sub _new { |
|
|
438 | my ($self, $fh, $pid) = @_; |
|
|
439 | |
|
|
440 | AnyEvent::Util::fh_nonblocking $fh, 1; |
|
|
441 | |
|
|
442 | $self = bless [ |
|
|
443 | $pid, |
|
|
444 | $fh, |
|
|
445 | [], # write queue - strings or fd's |
|
|
446 | undef, # AE watcher |
|
|
447 | ], $self; |
|
|
448 | |
|
|
449 | $self |
|
|
450 | } |
453 | } |
451 | |
454 | |
452 | # fork template from current process, used by AnyEvent::Fork::Early/Template |
455 | # fork template from current process, used by AnyEvent::Fork::Early/Template |
453 | sub _new_fork { |
456 | sub _new_fork { |
454 | my ($fh, $slave) = AnyEvent::Util::portable_socketpair; |
457 | my ($fh, $slave) = AnyEvent::Util::portable_socketpair; |
… | |
… | |
587 | AnyEvent::Fork itself. |
590 | AnyEvent::Fork itself. |
588 | |
591 | |
589 | =cut |
592 | =cut |
590 | |
593 | |
591 | sub pid { |
594 | sub pid { |
592 | $_[0][0] |
595 | $_[0][PID] |
593 | } |
596 | } |
594 | |
597 | |
595 | =item $proc = $proc->eval ($perlcode, @args) |
598 | =item $proc = $proc->eval ($perlcode, @args) |
596 | |
599 | |
597 | Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to |
600 | Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to |
… | |
… | |
664 | sub send_fh { |
667 | sub send_fh { |
665 | my ($self, @fh) = @_; |
668 | my ($self, @fh) = @_; |
666 | |
669 | |
667 | for my $fh (@fh) { |
670 | for my $fh (@fh) { |
668 | $self->_cmd ("h"); |
671 | $self->_cmd ("h"); |
669 | push @{ $self->[2] }, \$fh; |
672 | push @{ $self->[QUEUE] }, \$fh; |
670 | } |
673 | } |
671 | |
674 | |
672 | $self |
675 | $self |
673 | } |
676 | } |
674 | |
677 | |
… | |
… | |
760 | =cut |
763 | =cut |
761 | |
764 | |
762 | sub run { |
765 | sub run { |
763 | my ($self, $func, $cb) = @_; |
766 | my ($self, $func, $cb) = @_; |
764 | |
767 | |
765 | $self->[4] = $cb; |
768 | $self->[CB] = $cb; |
766 | $self->_cmd (r => $func); |
769 | $self->_cmd (r => $func); |
767 | } |
770 | } |
768 | |
771 | |
769 | =back |
772 | =back |
770 | |
773 | |
… | |
… | |
798 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
801 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
799 | though it uses the same operations, but adds a lot of overhead? |
802 | though it uses the same operations, but adds a lot of overhead? |
800 | |
803 | |
801 | The difference is simply the process size: forking the 5MB process takes |
804 | The difference is simply the process size: forking the 5MB process takes |
802 | so much longer than forking the 2.5MB template process that the extra |
805 | so much longer than forking the 2.5MB template process that the extra |
803 | overhead introduced is canceled out. |
806 | overhead is canceled out. |
804 | |
807 | |
805 | If the benchmark process grows, the normal fork becomes even slower: |
808 | If the benchmark process grows, the normal fork becomes even slower: |
806 | |
809 | |
807 | 1340 new processes, manual fork of a 20MB process |
810 | 1340 new processes, manual fork of a 20MB process |
808 | 731 new processes, manual fork of a 200MB process |
811 | 731 new processes, manual fork of a 200MB process |
… | |
… | |
907 | |
910 | |
908 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
911 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
909 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
912 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
910 | program at a convenient time). |
913 | program at a convenient time). |
911 | |
914 | |
912 | =head1 AUTHOR |
915 | =head1 AUTHOR AND CONTACT INFORMATION |
913 | |
916 | |
914 | Marc Lehmann <schmorp@schmorp.de> |
917 | Marc Lehmann <schmorp@schmorp.de> |
915 | http://home.schmorp.de/ |
918 | http://software.schmorp.de/pkg/AnyEvent-Fork |
916 | |
919 | |
917 | =cut |
920 | =cut |
918 | |
921 | |
919 | 1 |
922 | 1 |
920 | |
923 | |