ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork/Fork.pm
(Generate patch)

Comparing AnyEvent-Fork/Fork.pm (file contents):
Revision 1.34 by root, Sat Apr 6 09:34:53 2013 UTC vs.
Revision 1.41 by root, Mon Apr 8 03:20:53 2013 UTC

27 27
28Special care has been taken to make this module useful from other modules, 28Special care has been taken to make this module useful from other modules,
29while still supporting specialised environments such as L<App::Staticperl> 29while still supporting specialised environments such as L<App::Staticperl>
30or L<PAR::Packer>. 30or L<PAR::Packer>.
31 31
32=head1 WHAT THIS MODULE IS NOT 32=head2 WHAT THIS MODULE IS NOT
33 33
34This module only creates processes and lets you pass file handles and 34This module only creates processes and lets you pass file handles and
35strings to it, and run perl code. It does not implement any kind of RPC - 35strings to it, and run perl code. It does not implement any kind of RPC -
36there is no back channel from the process back to you, and there is no RPC 36there is no back channel from the process back to you, and there is no RPC
37or message passing going on. 37or message passing going on.
40in whatever way you like, use some message-passing module such 40in whatever way you like, use some message-passing module such
41as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use 41as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use
42L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, 42L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages,
43and so on. 43and so on.
44 44
45=head2 COMPARISON TO OTHER MODULES
46
47There is an abundance of modules on CPAN that do "something fork", such as
48L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker>
49or L<AnyEvent::Subprocess>. There are modules that implement their own
50process management, such as L<AnyEvent::DBI>.
51
52The problems that all these modules try to solve are real, however, none
53of them (from what I have seen) tackle the very real problems of unwanted
54memory sharing, efficiency, not being able to use event processing or
55similar modules in the processes they create.
56
57This module doesn't try to replace any of them - instead it tries to solve
58the problem of creating processes with a minimum of fuss and overhead (and
59also luxury). Ideally, most of these would use AnyEvent::Fork internally,
60except they were written before AnyEvent:Fork was available, so obviously
61had to roll their own.
62
45=head1 PROBLEM STATEMENT 63=head2 PROBLEM STATEMENT
46 64
47There are two traditional ways to implement parallel processing on UNIX 65There are two traditional ways to implement parallel processing on UNIX
48like operating systems - fork and process, and fork+exec and process. They 66like operating systems - fork and process, and fork+exec and process. They
49have different advantages and disadvantages that I describe below, 67have different advantages and disadvantages that I describe below,
50together with how this module tries to mitigate the disadvantages. 68together with how this module tries to mitigate the disadvantages.
212 open my $output, ">/tmp/log" or die "$!"; 230 open my $output, ">/tmp/log" or die "$!";
213 231
214 AnyEvent::Fork 232 AnyEvent::Fork
215 ->new 233 ->new
216 ->eval (' 234 ->eval ('
235 # compile a helper function for later use
217 sub run { 236 sub run {
218 my ($fh, $output, @cmd) = @_; 237 my ($fh, $output, @cmd) = @_;
219 238
220 # perl will clear close-on-exec on STDOUT/STDERR 239 # perl will clear close-on-exec on STDOUT/STDERR
221 open STDOUT, ">&", $output or die; 240 open STDOUT, ">&", $output or die;
351use AnyEvent; 370use AnyEvent;
352use AnyEvent::Util (); 371use AnyEvent::Util ();
353 372
354use IO::FDPass; 373use IO::FDPass;
355 374
356our $VERSION = 0.5; 375our $VERSION = 0.6;
357
358our $PERL; # the path to the perl interpreter, deduces with various forms of magic
359 376
360=over 4 377=over 4
361 378
362=back 379=back
363 380
442 if ($pid eq 0) { 459 if ($pid eq 0) {
443 require AnyEvent::Fork::Serve; 460 require AnyEvent::Fork::Serve;
444 $AnyEvent::Fork::Serve::OWNER = $parent; 461 $AnyEvent::Fork::Serve::OWNER = $parent;
445 close $fh; 462 close $fh;
446 $0 = "$_[1] of $parent"; 463 $0 = "$_[1] of $parent";
447 $SIG{CHLD} = 'IGNORE';
448 AnyEvent::Fork::Serve::serve ($slave); 464 AnyEvent::Fork::Serve::serve ($slave);
449 exit 0; 465 exit 0;
450 } elsif (!$pid) { 466 } elsif (!$pid) {
451 die "AnyEvent::Fork::Early/Template: unable to fork template process: $!"; 467 die "AnyEvent::Fork::Early/Template: unable to fork template process: $!";
452 } 468 }
627=item $proc = $proc->send_fh ($handle, ...) 643=item $proc = $proc->send_fh ($handle, ...)
628 644
629Send one or more file handles (I<not> file descriptors) to the process, 645Send one or more file handles (I<not> file descriptors) to the process,
630to prepare a call to C<run>. 646to prepare a call to C<run>.
631 647
632The process object keeps a reference to the handles until this is done, 648The process object keeps a reference to the handles until they have
633so you must not explicitly close the handles. This is most easily 649been passed over to the process, so you must not explicitly close the
634accomplished by simply not storing the file handles anywhere after passing 650handles. This is most easily accomplished by simply not storing the file
635them to this method. 651handles anywhere after passing them to this method - when AnyEvent::Fork
652is finished using them, perl will automatically close them.
636 653
637Returns the process object for easy chaining of method calls. 654Returns the process object for easy chaining of method calls.
638 655
639Example: pass a file handle to a process, and release it without 656Example: pass a file handle to a process, and release it without
640closing. It will be closed automatically when it is no longer used. 657closing. It will be closed automatically when it is no longer used.
656} 673}
657 674
658=item $proc = $proc->send_arg ($string, ...) 675=item $proc = $proc->send_arg ($string, ...)
659 676
660Send one or more argument strings to the process, to prepare a call to 677Send one or more argument strings to the process, to prepare a call to
661C<run>. The strings can be any octet string. 678C<run>. The strings can be any octet strings.
662 679
663The protocol is optimised to pass a moderate number of relatively short 680The protocol is optimised to pass a moderate number of relatively short
664strings - while you can pass up to 4GB of data in one go, this is more 681strings - while you can pass up to 4GB of data in one go, this is more
665meant to pass some ID information or other startup info, not big chunks of 682meant to pass some ID information or other startup info, not big chunks of
666data. 683data.
682Enter the function specified by the function name in C<$func> in the 699Enter the function specified by the function name in C<$func> in the
683process. The function is called with the communication socket as first 700process. The function is called with the communication socket as first
684argument, followed by all file handles and string arguments sent earlier 701argument, followed by all file handles and string arguments sent earlier
685via C<send_fh> and C<send_arg> methods, in the order they were called. 702via C<send_fh> and C<send_arg> methods, in the order they were called.
686 703
704The process object becomes unusable on return from this function - any
705further method calls result in undefined behaviour.
706
687The function name should be fully qualified, but if it isn't, it will be 707The function name should be fully qualified, but if it isn't, it will be
688looked up in the main package. 708looked up in the C<main> package.
689 709
690If the called function returns, doesn't exist, or any error occurs, the 710If the called function returns, doesn't exist, or any error occurs, the
691process exits. 711process exits.
692 712
693Preparing the process is done in the background - when all commands have 713Preparing the process is done in the background - when all commands have
694been sent, the callback is invoked with the local communications socket 714been sent, the callback is invoked with the local communications socket
695as argument. At this point you can start using the socket in any way you 715as argument. At this point you can start using the socket in any way you
696like. 716like.
697
698The process object becomes unusable on return from this function - any
699further method calls result in undefined behaviour.
700 717
701If the communication socket isn't used, it should be closed on both sides, 718If the communication socket isn't used, it should be closed on both sides,
702to save on kernel memory. 719to save on kernel memory.
703 720
704The socket is non-blocking in the parent, and blocking in the newly 721The socket is non-blocking in the parent, and blocking in the newly
779 479 vfork+execs per second, using AnyEvent::Fork->new_exec 796 479 vfork+execs per second, using AnyEvent::Fork->new_exec
780 797
781So how can C<< AnyEvent->new >> be faster than a standard fork, even 798So how can C<< AnyEvent->new >> be faster than a standard fork, even
782though it uses the same operations, but adds a lot of overhead? 799though it uses the same operations, but adds a lot of overhead?
783 800
784The difference is simply the process size: forking the 6MB process takes 801The difference is simply the process size: forking the 5MB process takes
785so much longer than forking the 2.5MB template process that the overhead 802so much longer than forking the 2.5MB template process that the extra
786introduced is canceled out. 803overhead introduced is canceled out.
787 804
788If the benchmark process grows, the normal fork becomes even slower: 805If the benchmark process grows, the normal fork becomes even slower:
789 806
790 1340 new processes, manual fork in a 20MB process 807 1340 new processes, manual fork of a 20MB process
791 731 new processes, manual fork in a 200MB process 808 731 new processes, manual fork of a 200MB process
792 235 new processes, manual fork in a 2000MB process 809 235 new processes, manual fork of a 2000MB process
793 810
794What that means (to me) is that I can use this module without having a 811What that means (to me) is that I can use this module without having a bad
795very bad conscience because of the extra overhead required to start new 812conscience because of the extra overhead required to start new processes.
796processes.
797 813
798=head1 TYPICAL PROBLEMS 814=head1 TYPICAL PROBLEMS
799 815
800This section lists typical problems that remain. I hope by recognising 816This section lists typical problems that remain. I hope by recognising
801them, most can be avoided. 817them, most can be avoided.
802 818
803=over 4 819=over 4
804 820
805=item "leaked" file descriptors for exec'ed processes 821=item leaked file descriptors for exec'ed processes
806 822
807POSIX systems inherit file descriptors by default when exec'ing a new 823POSIX systems inherit file descriptors by default when exec'ing a new
808process. While perl itself laudably sets the close-on-exec flags on new 824process. While perl itself laudably sets the close-on-exec flags on new
809file handles, most C libraries don't care, and even if all cared, it's 825file handles, most C libraries don't care, and even if all cared, it's
810often not possible to set the flag in a race-free manner. 826often not possible to set the flag in a race-free manner.
830libraries or the code that leaks those file descriptors. 846libraries or the code that leaks those file descriptors.
831 847
832Fortunately, most of these leaked descriptors do no harm, other than 848Fortunately, most of these leaked descriptors do no harm, other than
833sitting on some resources. 849sitting on some resources.
834 850
835=item "leaked" file descriptors for fork'ed processes 851=item leaked file descriptors for fork'ed processes
836 852
837Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, 853Normally, L<AnyEvent::Fork> does start new processes by exec'ing them,
838which closes file descriptors not marked for being inherited. 854which closes file descriptors not marked for being inherited.
839 855
840However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer 856However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer
849 865
850The solution is to either not load these modules before use'ing 866The solution is to either not load these modules before use'ing
851L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay 867L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay
852initialising them, for example, by calling C<init Gtk2> manually. 868initialising them, for example, by calling C<init Gtk2> manually.
853 869
854=item exit runs destructors 870=item exiting calls object destructors
855 871
856This only applies to users of Lc<AnyEvent::Fork:Early> and 872This only applies to users of L<AnyEvent::Fork:Early> and
857L<AnyEvent::Fork::Template>. 873L<AnyEvent::Fork::Template>, or when initialiasing code creates objects
874that reference external resources.
858 875
859When a process created by AnyEvent::Fork exits, it might do so by calling 876When a process created by AnyEvent::Fork exits, it might do so by calling
860exit, or simply letting perl reach the end of the program. At which point 877exit, or simply letting perl reach the end of the program. At which point
861Perl runs all destructors. 878Perl runs all destructors.
862 879
881to make it so, mostly due to the bloody broken perl that nobody seems to 898to make it so, mostly due to the bloody broken perl that nobody seems to
882care about. The fork emulation is a bad joke - I have yet to see something 899care about. The fork emulation is a bad joke - I have yet to see something
883useful that you can do with it without running into memory corruption 900useful that you can do with it without running into memory corruption
884issues or other braindamage. Hrrrr. 901issues or other braindamage. Hrrrr.
885 902
886Cygwin perl is not supported at the moment, as it should implement fd 903Cygwin perl is not supported at the moment due to some hilarious
887passing, but doesn't, and rolling my own is hard, as cygwin doesn't 904shortcomings of its API - see L<IO::FDPoll> for more details.
888support enough functionality to do it.
889 905
890=head1 SEE ALSO 906=head1 SEE ALSO
891 907
892L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), 908L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter),
893L<AnyEvent::Fork::Template> (to create a process by forking the main 909L<AnyEvent::Fork::Template> (to create a process by forking the main

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines