… | |
… | |
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. |
… | |
… | |
780 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
798 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
781 | |
799 | |
782 | 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 |
783 | 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? |
784 | |
802 | |
785 | 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 |
786 | 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 |
787 | introduced is canceled out. |
805 | overhead introduced is canceled out. |
788 | |
806 | |
789 | If the benchmark process grows, the normal fork becomes even slower: |
807 | If the benchmark process grows, the normal fork becomes even slower: |
790 | |
808 | |
791 | 1340 new processes, manual fork in a 20MB process |
809 | 1340 new processes, manual fork of a 20MB process |
792 | 731 new processes, manual fork in a 200MB process |
810 | 731 new processes, manual fork of a 200MB process |
793 | 235 new processes, manual fork in a 2000MB process |
811 | 235 new processes, manual fork of a 2000MB process |
794 | |
812 | |
795 | 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 |
796 | very bad conscience because of the extra overhead required to start new |
814 | conscience because of the extra overhead required to start new processes. |
797 | processes. |
|
|
798 | |
815 | |
799 | =head1 TYPICAL PROBLEMS |
816 | =head1 TYPICAL PROBLEMS |
800 | |
817 | |
801 | This section lists typical problems that remain. I hope by recognising |
818 | This section lists typical problems that remain. I hope by recognising |
802 | them, most can be avoided. |
819 | them, most can be avoided. |
803 | |
820 | |
804 | =over 4 |
821 | =over 4 |
805 | |
822 | |
806 | =item "leaked" file descriptors for exec'ed processes |
823 | =item leaked file descriptors for exec'ed processes |
807 | |
824 | |
808 | 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 |
809 | 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 |
810 | 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 |
811 | 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. |
… | |
… | |
831 | libraries or the code that leaks those file descriptors. |
848 | libraries or the code that leaks those file descriptors. |
832 | |
849 | |
833 | Fortunately, most of these leaked descriptors do no harm, other than |
850 | Fortunately, most of these leaked descriptors do no harm, other than |
834 | sitting on some resources. |
851 | sitting on some resources. |
835 | |
852 | |
836 | =item "leaked" file descriptors for fork'ed processes |
853 | =item leaked file descriptors for fork'ed processes |
837 | |
854 | |
838 | 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, |
839 | which closes file descriptors not marked for being inherited. |
856 | which closes file descriptors not marked for being inherited. |
840 | |
857 | |
841 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
858 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
… | |
… | |
850 | |
867 | |
851 | 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 |
852 | 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 |
853 | initialising them, for example, by calling C<init Gtk2> manually. |
870 | initialising them, for example, by calling C<init Gtk2> manually. |
854 | |
871 | |
855 | =item exit runs destructors |
872 | =item exiting calls object destructors |
856 | |
873 | |
857 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
874 | This only applies to users of L<AnyEvent::Fork:Early> and |
858 | L<AnyEvent::Fork::Template>. |
875 | L<AnyEvent::Fork::Template>, or when initialiasing code creates objects |
|
|
876 | that reference external resources. |
859 | |
877 | |
860 | 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 |
861 | 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 |
862 | Perl runs all destructors. |
880 | Perl runs all destructors. |
863 | |
881 | |
… | |
… | |
882 | 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 |
883 | 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 |
884 | 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 |
885 | issues or other braindamage. Hrrrr. |
903 | issues or other braindamage. Hrrrr. |
886 | |
904 | |
887 | 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 |
888 | 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. |
889 | support enough functionality to do it. |
|
|
890 | |
907 | |
891 | =head1 SEE ALSO |
908 | =head1 SEE ALSO |
892 | |
909 | |
893 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
910 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
894 | 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 |