… | |
… | |
255 | =head1 CONCEPTS |
255 | =head1 CONCEPTS |
256 | |
256 | |
257 | This module can create new processes either by executing a new perl |
257 | This module can create new processes either by executing a new perl |
258 | process, or by forking from an existing "template" process. |
258 | process, or by forking from an existing "template" process. |
259 | |
259 | |
|
|
260 | All these processes are called "child processes" (whether they are direct |
|
|
261 | children or not), while the process that manages them is called the |
|
|
262 | "parent process". |
|
|
263 | |
260 | Each such process comes with its own file handle that can be used to |
264 | Each such process comes with its own file handle that can be used to |
261 | communicate with it (it's actually a socket - one end in the new process, |
265 | communicate with it (it's actually a socket - one end in the new process, |
262 | one end in the main process), and among the things you can do in it are |
266 | one end in the main process), and among the things you can do in it are |
263 | load modules, fork new processes, send file handles to it, and execute |
267 | load modules, fork new processes, send file handles to it, and execute |
264 | functions. |
268 | functions. |
… | |
… | |
373 | use AnyEvent; |
377 | use AnyEvent; |
374 | use AnyEvent::Util (); |
378 | use AnyEvent::Util (); |
375 | |
379 | |
376 | use IO::FDPass; |
380 | use IO::FDPass; |
377 | |
381 | |
378 | our $VERSION = 0.6; |
382 | our $VERSION = 0.7; |
379 | |
383 | |
380 | # the early fork template process |
384 | # the early fork template process |
381 | our $EARLY; |
385 | our $EARLY; |
382 | |
386 | |
383 | # the empty template process |
387 | # the empty template process |
… | |
… | |
431 | # send string |
435 | # send string |
432 | my $len = syswrite $self->[FH], $self->[QUEUE][0]; |
436 | my $len = syswrite $self->[FH], $self->[QUEUE][0]; |
433 | |
437 | |
434 | unless ($len) { |
438 | unless ($len) { |
435 | return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; |
439 | return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; |
436 | undef $self->[3]; |
440 | undef $self->[WW]; |
437 | die "AnyEvent::Fork: command write failure: $!"; |
441 | die "AnyEvent::Fork: command write failure: $!"; |
438 | } |
442 | } |
439 | |
443 | |
440 | substr $self->[QUEUE][0], 0, $len, ""; |
444 | substr $self->[QUEUE][0], 0, $len, ""; |
441 | shift @{ $self->[QUEUE] } unless length $self->[QUEUE][0]; |
445 | shift @{ $self->[QUEUE] } unless length $self->[QUEUE][0]; |
… | |
… | |
444 | |
448 | |
445 | # everything written |
449 | # everything written |
446 | undef $self->[WW]; |
450 | undef $self->[WW]; |
447 | |
451 | |
448 | # invoke run callback, if any |
452 | # invoke run callback, if any |
|
|
453 | if ($self->[CB]) { |
449 | $self->[CB]->($self->[FH]) if $self->[CB]; |
454 | $self->[CB]->($self->[FH]); |
|
|
455 | @$self = (); |
|
|
456 | } |
450 | }; |
457 | }; |
451 | |
458 | |
452 | () # make sure we don't leak the watcher |
459 | () # make sure we don't leak the watcher |
453 | } |
460 | } |
454 | |
461 | |
… | |
… | |
767 | |
774 | |
768 | $self->[CB] = $cb; |
775 | $self->[CB] = $cb; |
769 | $self->_cmd (r => $func); |
776 | $self->_cmd (r => $func); |
770 | } |
777 | } |
771 | |
778 | |
|
|
779 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
|
|
780 | |
|
|
781 | Flushes all commands out to the process and then calls the callback with |
|
|
782 | the communications socket. |
|
|
783 | |
|
|
784 | The process object becomes unusable on return from this function - any |
|
|
785 | further method calls result in undefined behaviour. |
|
|
786 | |
|
|
787 | The point of this method is to give you a file handle thta you cna pass |
|
|
788 | to another process. In that other process, you can call C<new_from_fh |
|
|
789 | AnyEvent::Fork::RPC> to create a new C<AnyEvent::Fork> object from it, |
|
|
790 | thereby effectively passing a fork object to another process. |
|
|
791 | |
|
|
792 | =cut |
|
|
793 | |
|
|
794 | sub to_fh { |
|
|
795 | my ($self, $cb) = @_; |
|
|
796 | |
|
|
797 | $self->[CB] = $cb; |
|
|
798 | |
|
|
799 | unless ($self->[WW]) { |
|
|
800 | $self->[CB]->($self->[FH]); |
|
|
801 | @$self = (); |
|
|
802 | } |
|
|
803 | } |
|
|
804 | |
|
|
805 | =item new_from_fh AnyEvent::Fork $fh # EXPERIMENTAL, MIGHT BE REMOVED |
|
|
806 | |
|
|
807 | Takes a file handle originally rceeived by the C<to_fh> method and creates |
|
|
808 | a new C<AnyEvent:Fork> object. The child process itself will not change in |
|
|
809 | any way, i.e. it will keep all the modifications done to it before calling |
|
|
810 | C<to_fh>. |
|
|
811 | |
|
|
812 | The new object is very much like the original object, except that the |
|
|
813 | C<pid> method will return C<undef> even if the process is a direct child. |
|
|
814 | |
|
|
815 | =cut |
|
|
816 | |
|
|
817 | sub new_from_fh { |
|
|
818 | my ($class, $fh) = @_; |
|
|
819 | |
|
|
820 | $class->_new ($fh) |
|
|
821 | } |
|
|
822 | |
772 | =back |
823 | =back |
773 | |
824 | |
774 | =head1 PERFORMANCE |
825 | =head1 PERFORMANCE |
775 | |
826 | |
776 | Now for some unscientific benchmark numbers (all done on an amd64 |
827 | Now for some unscientific benchmark numbers (all done on an amd64 |
… | |
… | |
784 | |
835 | |
785 | 2079 new processes per second, using manual socketpair + fork |
836 | 2079 new processes per second, using manual socketpair + fork |
786 | |
837 | |
787 | Then I did the same thing, but instead of calling fork, I called |
838 | Then I did the same thing, but instead of calling fork, I called |
788 | AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
839 | AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
789 | socket form the child to close on exit. This does the same thing as manual |
840 | socket from the child to close on exit. This does the same thing as manual |
790 | socket pair + fork, except that what is forked is the template process |
841 | socket pair + fork, except that what is forked is the template process |
791 | (2440kB), and the socket needs to be passed to the server at the other end |
842 | (2440kB), and the socket needs to be passed to the server at the other end |
792 | of the socket first. |
843 | of the socket first. |
793 | |
844 | |
794 | 2307 new processes per second, using AnyEvent::Fork->new |
845 | 2307 new processes per second, using AnyEvent::Fork->new |
… | |
… | |
901 | to make it so, mostly due to the bloody broken perl that nobody seems to |
952 | to make it so, mostly due to the bloody broken perl that nobody seems to |
902 | care about. The fork emulation is a bad joke - I have yet to see something |
953 | care about. The fork emulation is a bad joke - I have yet to see something |
903 | useful that you can do with it without running into memory corruption |
954 | useful that you can do with it without running into memory corruption |
904 | issues or other braindamage. Hrrrr. |
955 | issues or other braindamage. Hrrrr. |
905 | |
956 | |
|
|
957 | Since fork is endlessly broken on win32 perls (it doesn't even remotely |
|
|
958 | work within it's documented limits) and quite obviously it's not getting |
|
|
959 | improved any time soon, the best way to proceed on windows would be to |
|
|
960 | always use C<new_exec> and thus never rely on perl's fork "emulation". |
|
|
961 | |
906 | Cygwin perl is not supported at the moment due to some hilarious |
962 | Cygwin perl is not supported at the moment due to some hilarious |
907 | shortcomings of its API - see L<IO::FDPoll> for more details. |
963 | shortcomings of its API - see L<IO::FDPoll> for more details. If you never |
|
|
964 | use C<send_fh> and always use C<new_exec> to create processes, it should |
|
|
965 | work though. |
908 | |
966 | |
909 | =head1 SEE ALSO |
967 | =head1 SEE ALSO |
910 | |
968 | |
911 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
969 | L<AnyEvent::Fork::Early>, to avoid executing a perl interpreter at all |
|
|
970 | (part of this distribution). |
|
|
971 | |
912 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
972 | L<AnyEvent::Fork::Template>, to create a process by forking the main |
913 | program at a convenient time), L<AnyEvent::Fork::RPC> (for simple RPC to |
973 | program at a convenient time (part of this distribution). |
914 | child processes). |
974 | |
|
|
975 | L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). |
915 | |
976 | |
916 | =head1 AUTHOR AND CONTACT INFORMATION |
977 | =head1 AUTHOR AND CONTACT INFORMATION |
917 | |
978 | |
918 | Marc Lehmann <schmorp@schmorp.de> |
979 | Marc Lehmann <schmorp@schmorp.de> |
919 | http://software.schmorp.de/pkg/AnyEvent-Fork |
980 | http://software.schmorp.de/pkg/AnyEvent-Fork |