… | |
… | |
27 | |
27 | |
28 | Special care has been taken to make this module useful from other modules, |
28 | Special care has been taken to make this module useful from other modules, |
29 | while still supporting specialised environments such as L<App::Staticperl> |
29 | while still supporting specialised environments such as L<App::Staticperl> |
30 | or L<PAR::Packer>. |
30 | or L<PAR::Packer>. |
31 | |
31 | |
32 | =head1 WHAT THIS MODULE IS NOT |
32 | =head2 WHAT THIS MODULE IS NOT |
33 | |
33 | |
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. |
… | |
… | |
40 | in whatever way you like, use some message-passing module such |
40 | in whatever way you like, use some message-passing module such |
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
43 | and so on. |
43 | and so on. |
44 | |
44 | |
|
|
45 | =head2 COMPARISON TO OTHER MODULES |
|
|
46 | |
|
|
47 | There is an abundance of modules on CPAN that do "something fork", such as |
|
|
48 | L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker> |
|
|
49 | or L<AnyEvent::Subprocess>. There are modules that implement their own |
|
|
50 | process management, such as L<AnyEvent::DBI>. |
|
|
51 | |
|
|
52 | The problems that all these modules try to solve are real, however, none |
|
|
53 | of them (from what I have seen) tackle the very real problems of unwanted |
|
|
54 | memory sharing, efficiency, not being able to use event processing or |
|
|
55 | similar modules in the processes they create. |
|
|
56 | |
|
|
57 | This module doesn't try to replace any of them - instead it tries to solve |
|
|
58 | the problem of creating processes with a minimum of fuss and overhead (and |
|
|
59 | also luxury). Ideally, most of these would use AnyEvent::Fork internally, |
|
|
60 | except they were written before AnyEvent:Fork was available, so obviously |
|
|
61 | had to roll their own. |
|
|
62 | |
45 | =head1 PROBLEM STATEMENT |
63 | =head2 PROBLEM STATEMENT |
46 | |
64 | |
47 | There are two traditional ways to implement parallel processing on UNIX |
65 | There are two traditional ways to implement parallel processing on UNIX |
48 | like operating systems - fork and process, and fork+exec and process. They |
66 | like operating systems - fork and process, and fork+exec and process. They |
49 | have different advantages and disadvantages that I describe below, |
67 | have different advantages and disadvantages that I describe below, |
50 | together with how this module tries to mitigate the disadvantages. |
68 | together with how this module tries to mitigate the disadvantages. |
… | |
… | |
152 | |
170 | |
153 | # now $master_filehandle is connected to the |
171 | # now $master_filehandle is connected to the |
154 | # $slave_filehandle in the new process. |
172 | # $slave_filehandle in the new process. |
155 | }); |
173 | }); |
156 | |
174 | |
157 | MyModule::worker might look like this: |
175 | C<MyModule> might look like this: |
158 | |
176 | |
|
|
177 | package MyModule; |
|
|
178 | |
159 | sub MyModule::worker { |
179 | sub worker { |
160 | my ($slave_filehandle) = @_; |
180 | my ($slave_filehandle) = @_; |
161 | |
181 | |
162 | # now $slave_filehandle is connected to the $master_filehandle |
182 | # now $slave_filehandle is connected to the $master_filehandle |
163 | # in the original prorcess. have fun! |
183 | # in the original prorcess. have fun! |
164 | } |
184 | } |
… | |
… | |
183 | } |
203 | } |
184 | |
204 | |
185 | # now do other things - maybe use the filehandle provided by run |
205 | # now do other things - maybe use the filehandle provided by run |
186 | # to wait for the processes to die. or whatever. |
206 | # to wait for the processes to die. or whatever. |
187 | |
207 | |
188 | My::Server::run might look like this: |
208 | C<My::Server> might look like this: |
189 | |
209 | |
190 | sub My::Server::run { |
210 | package My::Server; |
|
|
211 | |
|
|
212 | sub run { |
191 | my ($slave, $listener, $id) = @_; |
213 | my ($slave, $listener, $id) = @_; |
192 | |
214 | |
193 | close $slave; # we do not use the socket, so close it to save resources |
215 | close $slave; # we do not use the socket, so close it to save resources |
194 | |
216 | |
195 | # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
217 | # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
… | |
… | |
199 | } |
221 | } |
200 | } |
222 | } |
201 | |
223 | |
202 | =head2 use AnyEvent::Fork as a faster fork+exec |
224 | =head2 use AnyEvent::Fork as a faster fork+exec |
203 | |
225 | |
204 | This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to |
226 | This runs C</bin/echo hi>, with stdandard output redirected to /tmp/log |
205 | the communications socket. It is usually faster than fork+exec, but still |
227 | and standard error redirected to the communications socket. It is usually |
206 | let's you prepare the environment. |
228 | faster than fork+exec, but still lets you prepare the environment. |
207 | |
229 | |
208 | open my $output, ">/tmp/log" or die "$!"; |
230 | open my $output, ">/tmp/log" or die "$!"; |
209 | |
231 | |
210 | AnyEvent::Fork |
232 | AnyEvent::Fork |
211 | ->new |
233 | ->new |
… | |
… | |
555 | } |
577 | } |
556 | |
578 | |
557 | =item $pid = $proc->pid |
579 | =item $pid = $proc->pid |
558 | |
580 | |
559 | Returns the process id of the process I<iff it is a direct child of the |
581 | Returns the process id of the process I<iff it is a direct child of the |
560 | process> running AnyEvent::Fork, and C<undef> otherwise. |
582 | process running AnyEvent::Fork>, and C<undef> otherwise. |
561 | |
583 | |
562 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
584 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
563 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
585 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
564 | to clean up their zombies when they die. |
586 | to clean up their zombies when they die. |
565 | |
587 | |
… | |
… | |
583 | |
605 | |
584 | The code will usually be executed after this call returns, and there is no |
606 | The code will usually be executed after this call returns, and there is no |
585 | way to pass anything back to the calling process. Any evaluation errors |
607 | way to pass anything back to the calling process. Any evaluation errors |
586 | will be reported to stderr and cause the process to exit. |
608 | will be reported to stderr and cause the process to exit. |
587 | |
609 | |
588 | If you want to execute some code to take over the process (see the |
610 | If you want to execute some code (that isn't in a module) to take over the |
589 | "fork+exec" example in the SYNOPSIS), you should compile a function via |
611 | process, you should compile a function via C<eval> first, and then call |
590 | C<eval> first, and then call it via C<run>. This also gives you access to |
612 | it via C<run>. This also gives you access to any arguments passed via the |
591 | any arguments passed via the C<send_xxx> methods, such as file handles. |
613 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
|
|
614 | a faster fork+exec> example to see it in action. |
592 | |
615 | |
593 | Returns the process object for easy chaining of method calls. |
616 | Returns the process object for easy chaining of method calls. |
594 | |
617 | |
595 | =cut |
618 | =cut |
596 | |
619 | |
… | |
… | |
622 | =item $proc = $proc->send_fh ($handle, ...) |
645 | =item $proc = $proc->send_fh ($handle, ...) |
623 | |
646 | |
624 | Send one or more file handles (I<not> file descriptors) to the process, |
647 | Send one or more file handles (I<not> file descriptors) to the process, |
625 | to prepare a call to C<run>. |
648 | to prepare a call to C<run>. |
626 | |
649 | |
627 | The process object keeps a reference to the handles until this is done, |
650 | The process object keeps a reference to the handles until they have |
628 | so you must not explicitly close the handles. This is most easily |
651 | been passed over to the process, so you must not explicitly close the |
629 | accomplished by simply not storing the file handles anywhere after passing |
652 | handles. This is most easily accomplished by simply not storing the file |
630 | them to this method. |
653 | handles anywhere after passing them to this method - when AnyEvent::Fork |
|
|
654 | is finished using them, perl will automatically close them. |
631 | |
655 | |
632 | Returns the process object for easy chaining of method calls. |
656 | Returns the process object for easy chaining of method calls. |
633 | |
657 | |
634 | Example: pass a file handle to a process, and release it without |
658 | Example: pass a file handle to a process, and release it without |
635 | closing. It will be closed automatically when it is no longer used. |
659 | closing. It will be closed automatically when it is no longer used. |
… | |
… | |
651 | } |
675 | } |
652 | |
676 | |
653 | =item $proc = $proc->send_arg ($string, ...) |
677 | =item $proc = $proc->send_arg ($string, ...) |
654 | |
678 | |
655 | Send one or more argument strings to the process, to prepare a call to |
679 | Send one or more argument strings to the process, to prepare a call to |
656 | C<run>. The strings can be any octet string. |
680 | C<run>. The strings can be any octet strings. |
657 | |
681 | |
658 | The protocol is optimised to pass a moderate number of relatively short |
682 | The protocol is optimised to pass a moderate number of relatively short |
659 | strings - while you can pass up to 4GB of data in one go, this is more |
683 | strings - while you can pass up to 4GB of data in one go, this is more |
660 | meant to pass some ID information or other startup info, not big chunks of |
684 | meant to pass some ID information or other startup info, not big chunks of |
661 | data. |
685 | data. |
… | |
… | |
677 | Enter the function specified by the function name in C<$func> in the |
701 | Enter the function specified by the function name in C<$func> in the |
678 | process. The function is called with the communication socket as first |
702 | process. The function is called with the communication socket as first |
679 | argument, followed by all file handles and string arguments sent earlier |
703 | argument, followed by all file handles and string arguments sent earlier |
680 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
704 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
681 | |
705 | |
|
|
706 | The process object becomes unusable on return from this function - any |
|
|
707 | further method calls result in undefined behaviour. |
|
|
708 | |
682 | The function name should be fully qualified, but if it isn't, it will be |
709 | The function name should be fully qualified, but if it isn't, it will be |
683 | looked up in the main package. |
710 | looked up in the C<main> package. |
684 | |
711 | |
685 | If the called function returns, doesn't exist, or any error occurs, the |
712 | If the called function returns, doesn't exist, or any error occurs, the |
686 | process exits. |
713 | process exits. |
687 | |
714 | |
688 | Preparing the process is done in the background - when all commands have |
715 | Preparing the process is done in the background - when all commands have |
689 | been sent, the callback is invoked with the local communications socket |
716 | been sent, the callback is invoked with the local communications socket |
690 | as argument. At this point you can start using the socket in any way you |
717 | as argument. At this point you can start using the socket in any way you |
691 | like. |
718 | like. |
692 | |
|
|
693 | The process object becomes unusable on return from this function - any |
|
|
694 | further method calls result in undefined behaviour. |
|
|
695 | |
719 | |
696 | If the communication socket isn't used, it should be closed on both sides, |
720 | If the communication socket isn't used, it should be closed on both sides, |
697 | to save on kernel memory. |
721 | to save on kernel memory. |
698 | |
722 | |
699 | The socket is non-blocking in the parent, and blocking in the newly |
723 | The socket is non-blocking in the parent, and blocking in the newly |
… | |
… | |
774 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
798 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
775 | |
799 | |
776 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
800 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
777 | though it uses the same operations, but adds a lot of overhead? |
801 | though it uses the same operations, but adds a lot of overhead? |
778 | |
802 | |
779 | The difference is simply the process size: forking the 6MB process takes |
803 | The difference is simply the process size: forking the 5MB process takes |
780 | so much longer than forking the 2.5MB template process that the overhead |
804 | so much longer than forking the 2.5MB template process that the extra |
781 | introduced is canceled out. |
805 | overhead introduced is canceled out. |
782 | |
806 | |
783 | If the benchmark process grows, the normal fork becomes even slower: |
807 | If the benchmark process grows, the normal fork becomes even slower: |
784 | |
808 | |
785 | 1340 new processes, manual fork in a 20MB process |
809 | 1340 new processes, manual fork of a 20MB process |
786 | 731 new processes, manual fork in a 200MB process |
810 | 731 new processes, manual fork of a 200MB process |
787 | 235 new processes, manual fork in a 2000MB process |
811 | 235 new processes, manual fork of a 2000MB process |
788 | |
812 | |
789 | What that means (to me) is that I can use this module without having a |
813 | What that means (to me) is that I can use this module without having a bad |
790 | very bad conscience because of the extra overhead required to start new |
814 | conscience because of the extra overhead required to start new processes. |
791 | processes. |
|
|
792 | |
815 | |
793 | =head1 TYPICAL PROBLEMS |
816 | =head1 TYPICAL PROBLEMS |
794 | |
817 | |
795 | This section lists typical problems that remain. I hope by recognising |
818 | This section lists typical problems that remain. I hope by recognising |
796 | them, most can be avoided. |
819 | them, most can be avoided. |
797 | |
820 | |
798 | =over 4 |
821 | =over 4 |
799 | |
822 | |
800 | =item "leaked" file descriptors for exec'ed processes |
823 | =item leaked file descriptors for exec'ed processes |
801 | |
824 | |
802 | POSIX systems inherit file descriptors by default when exec'ing a new |
825 | POSIX systems inherit file descriptors by default when exec'ing a new |
803 | process. While perl itself laudably sets the close-on-exec flags on new |
826 | process. While perl itself laudably sets the close-on-exec flags on new |
804 | file handles, most C libraries don't care, and even if all cared, it's |
827 | file handles, most C libraries don't care, and even if all cared, it's |
805 | often not possible to set the flag in a race-free manner. |
828 | often not possible to set the flag in a race-free manner. |
… | |
… | |
825 | libraries or the code that leaks those file descriptors. |
848 | libraries or the code that leaks those file descriptors. |
826 | |
849 | |
827 | Fortunately, most of these leaked descriptors do no harm, other than |
850 | Fortunately, most of these leaked descriptors do no harm, other than |
828 | sitting on some resources. |
851 | sitting on some resources. |
829 | |
852 | |
830 | =item "leaked" file descriptors for fork'ed processes |
853 | =item leaked file descriptors for fork'ed processes |
831 | |
854 | |
832 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
855 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
833 | which closes file descriptors not marked for being inherited. |
856 | which closes file descriptors not marked for being inherited. |
834 | |
857 | |
835 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
858 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
… | |
… | |
844 | |
867 | |
845 | The solution is to either not load these modules before use'ing |
868 | The solution is to either not load these modules before use'ing |
846 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay |
869 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay |
847 | initialising them, for example, by calling C<init Gtk2> manually. |
870 | initialising them, for example, by calling C<init Gtk2> manually. |
848 | |
871 | |
849 | =item exit runs destructors |
872 | =item exiting calls object destructors |
850 | |
873 | |
851 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
874 | This only applies to users of L<AnyEvent::Fork:Early> and |
852 | L<AnyEvent::Fork::Template>. |
875 | L<AnyEvent::Fork::Template>, or when initialiasing code creates objects |
|
|
876 | that reference external resources. |
853 | |
877 | |
854 | When a process created by AnyEvent::Fork exits, it might do so by calling |
878 | When a process created by AnyEvent::Fork exits, it might do so by calling |
855 | exit, or simply letting perl reach the end of the program. At which point |
879 | exit, or simply letting perl reach the end of the program. At which point |
856 | Perl runs all destructors. |
880 | Perl runs all destructors. |
857 | |
881 | |
… | |
… | |
876 | to make it so, mostly due to the bloody broken perl that nobody seems to |
900 | to make it so, mostly due to the bloody broken perl that nobody seems to |
877 | care about. The fork emulation is a bad joke - I have yet to see something |
901 | care about. The fork emulation is a bad joke - I have yet to see something |
878 | useful that you can do with it without running into memory corruption |
902 | useful that you can do with it without running into memory corruption |
879 | issues or other braindamage. Hrrrr. |
903 | issues or other braindamage. Hrrrr. |
880 | |
904 | |
881 | Cygwin perl is not supported at the moment, as it should implement fd |
905 | Cygwin perl is not supported at the moment due to some hilarious |
882 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
906 | shortcomings of its API - see L<IO::FDPoll> for more details. |
883 | support enough functionality to do it. |
|
|
884 | |
907 | |
885 | =head1 SEE ALSO |
908 | =head1 SEE ALSO |
886 | |
909 | |
887 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
910 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
888 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
911 | L<AnyEvent::Fork::Template> (to create a process by forking the main |