… | |
… | |
152 | |
152 | |
153 | # now $master_filehandle is connected to the |
153 | # now $master_filehandle is connected to the |
154 | # $slave_filehandle in the new process. |
154 | # $slave_filehandle in the new process. |
155 | }); |
155 | }); |
156 | |
156 | |
157 | MyModule might look like this: |
157 | C<MyModule> might look like this: |
158 | |
158 | |
159 | package MyModule; |
159 | package MyModule; |
160 | |
160 | |
161 | sub worker { |
161 | sub worker { |
162 | my ($slave_filehandle) = @_; |
162 | my ($slave_filehandle) = @_; |
… | |
… | |
185 | } |
185 | } |
186 | |
186 | |
187 | # now do other things - maybe use the filehandle provided by run |
187 | # now do other things - maybe use the filehandle provided by run |
188 | # to wait for the processes to die. or whatever. |
188 | # to wait for the processes to die. or whatever. |
189 | |
189 | |
190 | My::Server might look like this: |
190 | C<My::Server> might look like this: |
191 | |
191 | |
192 | package My::Server; |
192 | package My::Server; |
193 | |
193 | |
194 | sub run { |
194 | sub run { |
195 | my ($slave, $listener, $id) = @_; |
195 | my ($slave, $listener, $id) = @_; |
… | |
… | |
203 | } |
203 | } |
204 | } |
204 | } |
205 | |
205 | |
206 | =head2 use AnyEvent::Fork as a faster fork+exec |
206 | =head2 use AnyEvent::Fork as a faster fork+exec |
207 | |
207 | |
208 | This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to |
208 | This runs C</bin/echo hi>, with stdandard output redirected to /tmp/log |
209 | the communications socket. It is usually faster than fork+exec, but still |
209 | and standard error redirected to the communications socket. It is usually |
210 | let's you prepare the environment. |
210 | faster than fork+exec, but still lets you prepare the environment. |
211 | |
211 | |
212 | open my $output, ">/tmp/log" or die "$!"; |
212 | open my $output, ">/tmp/log" or die "$!"; |
213 | |
213 | |
214 | AnyEvent::Fork |
214 | AnyEvent::Fork |
215 | ->new |
215 | ->new |
… | |
… | |
559 | } |
559 | } |
560 | |
560 | |
561 | =item $pid = $proc->pid |
561 | =item $pid = $proc->pid |
562 | |
562 | |
563 | Returns the process id of the process I<iff it is a direct child of the |
563 | Returns the process id of the process I<iff it is a direct child of the |
564 | process> running AnyEvent::Fork, and C<undef> otherwise. |
564 | process running AnyEvent::Fork>, and C<undef> otherwise. |
565 | |
565 | |
566 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
566 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
567 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
567 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
568 | to clean up their zombies when they die. |
568 | to clean up their zombies when they die. |
569 | |
569 | |
… | |
… | |
587 | |
587 | |
588 | The code will usually be executed after this call returns, and there is no |
588 | The code will usually be executed after this call returns, and there is no |
589 | way to pass anything back to the calling process. Any evaluation errors |
589 | way to pass anything back to the calling process. Any evaluation errors |
590 | will be reported to stderr and cause the process to exit. |
590 | will be reported to stderr and cause the process to exit. |
591 | |
591 | |
592 | If you want to execute some code to take over the process (see the |
592 | If you want to execute some code (that isn't in a module) to take over the |
593 | "fork+exec" example in the SYNOPSIS), you should compile a function via |
593 | process, you should compile a function via C<eval> first, and then call |
594 | C<eval> first, and then call it via C<run>. This also gives you access to |
594 | it via C<run>. This also gives you access to any arguments passed via the |
595 | any arguments passed via the C<send_xxx> methods, such as file handles. |
595 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
|
|
596 | a faster fork+exec> example to see it in action. |
596 | |
597 | |
597 | Returns the process object for easy chaining of method calls. |
598 | Returns the process object for easy chaining of method calls. |
598 | |
599 | |
599 | =cut |
600 | =cut |
600 | |
601 | |
… | |
… | |
626 | =item $proc = $proc->send_fh ($handle, ...) |
627 | =item $proc = $proc->send_fh ($handle, ...) |
627 | |
628 | |
628 | Send one or more file handles (I<not> file descriptors) to the process, |
629 | Send one or more file handles (I<not> file descriptors) to the process, |
629 | to prepare a call to C<run>. |
630 | to prepare a call to C<run>. |
630 | |
631 | |
631 | The process object keeps a reference to the handles until this is done, |
632 | The process object keeps a reference to the handles until they have |
632 | so you must not explicitly close the handles. This is most easily |
633 | been passed over to the process, so you must not explicitly close the |
633 | accomplished by simply not storing the file handles anywhere after passing |
634 | handles. This is most easily accomplished by simply not storing the file |
634 | them to this method. |
635 | handles anywhere after passing them to this method - when AnyEvent::Fork |
|
|
636 | is finished using them, perl will automatically close them. |
635 | |
637 | |
636 | Returns the process object for easy chaining of method calls. |
638 | Returns the process object for easy chaining of method calls. |
637 | |
639 | |
638 | Example: pass a file handle to a process, and release it without |
640 | Example: pass a file handle to a process, and release it without |
639 | closing. It will be closed automatically when it is no longer used. |
641 | closing. It will be closed automatically when it is no longer used. |
… | |
… | |
655 | } |
657 | } |
656 | |
658 | |
657 | =item $proc = $proc->send_arg ($string, ...) |
659 | =item $proc = $proc->send_arg ($string, ...) |
658 | |
660 | |
659 | Send one or more argument strings to the process, to prepare a call to |
661 | Send one or more argument strings to the process, to prepare a call to |
660 | C<run>. The strings can be any octet string. |
662 | C<run>. The strings can be any octet strings. |
661 | |
663 | |
662 | The protocol is optimised to pass a moderate number of relatively short |
664 | The protocol is optimised to pass a moderate number of relatively short |
663 | strings - while you can pass up to 4GB of data in one go, this is more |
665 | strings - while you can pass up to 4GB of data in one go, this is more |
664 | meant to pass some ID information or other startup info, not big chunks of |
666 | meant to pass some ID information or other startup info, not big chunks of |
665 | data. |
667 | data. |
… | |
… | |
681 | Enter the function specified by the function name in C<$func> in the |
683 | Enter the function specified by the function name in C<$func> in the |
682 | process. The function is called with the communication socket as first |
684 | process. The function is called with the communication socket as first |
683 | argument, followed by all file handles and string arguments sent earlier |
685 | argument, followed by all file handles and string arguments sent earlier |
684 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
686 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
685 | |
687 | |
|
|
688 | The process object becomes unusable on return from this function - any |
|
|
689 | further method calls result in undefined behaviour. |
|
|
690 | |
686 | The function name should be fully qualified, but if it isn't, it will be |
691 | The function name should be fully qualified, but if it isn't, it will be |
687 | looked up in the main package. |
692 | looked up in the C<main> package. |
688 | |
693 | |
689 | If the called function returns, doesn't exist, or any error occurs, the |
694 | If the called function returns, doesn't exist, or any error occurs, the |
690 | process exits. |
695 | process exits. |
691 | |
696 | |
692 | Preparing the process is done in the background - when all commands have |
697 | Preparing the process is done in the background - when all commands have |
693 | been sent, the callback is invoked with the local communications socket |
698 | been sent, the callback is invoked with the local communications socket |
694 | as argument. At this point you can start using the socket in any way you |
699 | as argument. At this point you can start using the socket in any way you |
695 | like. |
700 | like. |
696 | |
|
|
697 | The process object becomes unusable on return from this function - any |
|
|
698 | further method calls result in undefined behaviour. |
|
|
699 | |
701 | |
700 | If the communication socket isn't used, it should be closed on both sides, |
702 | If the communication socket isn't used, it should be closed on both sides, |
701 | to save on kernel memory. |
703 | to save on kernel memory. |
702 | |
704 | |
703 | The socket is non-blocking in the parent, and blocking in the newly |
705 | The socket is non-blocking in the parent, and blocking in the newly |
… | |
… | |
778 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
780 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
779 | |
781 | |
780 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
782 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
781 | though it uses the same operations, but adds a lot of overhead? |
783 | though it uses the same operations, but adds a lot of overhead? |
782 | |
784 | |
783 | The difference is simply the process size: forking the 6MB process takes |
785 | The difference is simply the process size: forking the 5MB process takes |
784 | so much longer than forking the 2.5MB template process that the overhead |
786 | so much longer than forking the 2.5MB template process that the extra |
785 | introduced is canceled out. |
787 | overhead introduced is canceled out. |
786 | |
788 | |
787 | If the benchmark process grows, the normal fork becomes even slower: |
789 | If the benchmark process grows, the normal fork becomes even slower: |
788 | |
790 | |
789 | 1340 new processes, manual fork in a 20MB process |
791 | 1340 new processes, manual fork of a 20MB process |
790 | 731 new processes, manual fork in a 200MB process |
792 | 731 new processes, manual fork of a 200MB process |
791 | 235 new processes, manual fork in a 2000MB process |
793 | 235 new processes, manual fork of a 2000MB process |
792 | |
794 | |
793 | What that means (to me) is that I can use this module without having a |
795 | What that means (to me) is that I can use this module without having a bad |
794 | very bad conscience because of the extra overhead required to start new |
796 | conscience because of the extra overhead required to start new processes. |
795 | processes. |
|
|
796 | |
797 | |
797 | =head1 TYPICAL PROBLEMS |
798 | =head1 TYPICAL PROBLEMS |
798 | |
799 | |
799 | This section lists typical problems that remain. I hope by recognising |
800 | This section lists typical problems that remain. I hope by recognising |
800 | them, most can be avoided. |
801 | them, most can be avoided. |
801 | |
802 | |
802 | =over 4 |
803 | =over 4 |
803 | |
804 | |
804 | =item "leaked" file descriptors for exec'ed processes |
805 | =item leaked file descriptors for exec'ed processes |
805 | |
806 | |
806 | POSIX systems inherit file descriptors by default when exec'ing a new |
807 | POSIX systems inherit file descriptors by default when exec'ing a new |
807 | process. While perl itself laudably sets the close-on-exec flags on new |
808 | process. While perl itself laudably sets the close-on-exec flags on new |
808 | file handles, most C libraries don't care, and even if all cared, it's |
809 | file handles, most C libraries don't care, and even if all cared, it's |
809 | often not possible to set the flag in a race-free manner. |
810 | often not possible to set the flag in a race-free manner. |
… | |
… | |
829 | libraries or the code that leaks those file descriptors. |
830 | libraries or the code that leaks those file descriptors. |
830 | |
831 | |
831 | Fortunately, most of these leaked descriptors do no harm, other than |
832 | Fortunately, most of these leaked descriptors do no harm, other than |
832 | sitting on some resources. |
833 | sitting on some resources. |
833 | |
834 | |
834 | =item "leaked" file descriptors for fork'ed processes |
835 | =item leaked file descriptors for fork'ed processes |
835 | |
836 | |
836 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
837 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
837 | which closes file descriptors not marked for being inherited. |
838 | which closes file descriptors not marked for being inherited. |
838 | |
839 | |
839 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
840 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
… | |
… | |
880 | to make it so, mostly due to the bloody broken perl that nobody seems to |
881 | to make it so, mostly due to the bloody broken perl that nobody seems to |
881 | care about. The fork emulation is a bad joke - I have yet to see something |
882 | care about. The fork emulation is a bad joke - I have yet to see something |
882 | useful that you can do with it without running into memory corruption |
883 | useful that you can do with it without running into memory corruption |
883 | issues or other braindamage. Hrrrr. |
884 | issues or other braindamage. Hrrrr. |
884 | |
885 | |
885 | Cygwin perl is not supported at the moment, as it should implement fd |
886 | Cygwin perl is not supported at the moment due to some hilarious |
886 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
887 | shortcomings of its API - see L<IO::FDPoll> for more details. |
887 | support enough functionality to do it. |
|
|
888 | |
888 | |
889 | =head1 SEE ALSO |
889 | =head1 SEE ALSO |
890 | |
890 | |
891 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
891 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
892 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
892 | L<AnyEvent::Fork::Template> (to create a process by forking the main |