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.27 by root, Sat Apr 6 09:05:50 2013 UTC vs.
Revision 1.38 by root, Sat Apr 6 20:07:30 2013 UTC

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::worker might look like this 157C<MyModule> might look like this:
158
159 package MyModule;
160
158 sub MyModule::worker { 161 sub worker {
159 my ($slave_filehandle) = @_; 162 my ($slave_filehandle) = @_;
160 163
161 # now $slave_filehandle is connected to the $master_filehandle 164 # now $slave_filehandle is connected to the $master_filehandle
162 # in the original prorcess. have fun! 165 # in the original prorcess. have fun!
163 } 166 }
182 } 185 }
183 186
184 # now do other things - maybe use the filehandle provided by run 187 # now do other things - maybe use the filehandle provided by run
185 # to wait for the processes to die. or whatever. 188 # to wait for the processes to die. or whatever.
186 189
187 # My::Server::run might look like this 190C<My::Server> might look like this:
188 sub My::Server::run { 191
192 package My::Server;
193
194 sub run {
189 my ($slave, $listener, $id) = @_; 195 my ($slave, $listener, $id) = @_;
190 196
191 close $slave; # we do not use the socket, so close it to save resources 197 close $slave; # we do not use the socket, so close it to save resources
192 198
193 # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, 199 # we could go ballistic and use e.g. AnyEvent here, or IO::AIO,
197 } 203 }
198 } 204 }
199 205
200=head2 use AnyEvent::Fork as a faster fork+exec 206=head2 use AnyEvent::Fork as a faster fork+exec
201 207
202This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to 208This runs C</bin/echo hi>, with stdandard output redirected to /tmp/log
203the communications socket. It is usually faster than fork+exec, but still 209and standard error redirected to the communications socket. It is usually
204let's you prepare the environment. 210faster than fork+exec, but still lets you prepare the environment.
205 211
206 open my $output, ">/tmp/log" or die "$!"; 212 open my $output, ">/tmp/log" or die "$!";
207 213
208 AnyEvent::Fork 214 AnyEvent::Fork
209 ->new 215 ->new
314=head1 THE C<AnyEvent::Fork> CLASS 320=head1 THE C<AnyEvent::Fork> CLASS
315 321
316This module exports nothing, and only implements a single class - 322This module exports nothing, and only implements a single class -
317C<AnyEvent::Fork>. 323C<AnyEvent::Fork>.
318 324
319There are two constructors that both create new processes - C<new> and 325There are two class constructors that both create new processes - C<new>
320C<new_exec>. The C<fork> method creates a new process by forking an 326and C<new_exec>. The C<fork> method creates a new process by forking an
321existing one and could be considered a third constructor. 327existing one and could be considered a third constructor.
322 328
323Most of the remaining methods deal with preparing the new process, by 329Most of the remaining methods deal with preparing the new process, by
324loading code, evaluating code and sending data to the new process. They 330loading code, evaluating code and sending data to the new process. They
325usually return the process object, so you can chain method calls. 331usually return the process object, so you can chain method calls.
326 332
327If a process object is destroyed before calling its C<run> method, then 333If a process object is destroyed before calling its C<run> method, then
328the process simply exits. After C<run> is called, all responsibility is 334the process simply exits. After C<run> is called, all responsibility is
329passed to the specified function. 335passed to the specified function.
330 336
337As long as there is any outstanding work to be done, process objects
338resist being destroyed, so there is no reason to store them unless you
339need them later - configure and forget works just fine.
340
331=over 4 341=over 4
332 342
333=cut 343=cut
334 344
335package AnyEvent::Fork; 345package AnyEvent::Fork;
344use IO::FDPass; 354use IO::FDPass;
345 355
346our $VERSION = 0.5; 356our $VERSION = 0.5;
347 357
348our $PERL; # the path to the perl interpreter, deduces with various forms of magic 358our $PERL; # the path to the perl interpreter, deduces with various forms of magic
349
350=item my $pool = new AnyEvent::Fork key => value...
351
352Create a new process pool. The following named parameters are supported:
353 359
354=over 4 360=over 4
355 361
356=back 362=back
357 363
453Create a new "empty" perl interpreter process and returns its process 459Create a new "empty" perl interpreter process and returns its process
454object for further manipulation. 460object for further manipulation.
455 461
456The new process is forked from a template process that is kept around 462The new process is forked from a template process that is kept around
457for this purpose. When it doesn't exist yet, it is created by a call to 463for this purpose. When it doesn't exist yet, it is created by a call to
458C<new_exec> and kept around for future calls. 464C<new_exec> first and then stays around for future calls.
459
460When the process object is destroyed, it will release the file handle
461that connects it with the new process. When the new process has not yet
462called C<run>, then the process will exit. Otherwise, what happens depends
463entirely on the code that is executed.
464 465
465=cut 466=cut
466 467
467sub new { 468sub new {
468 my $class = shift; 469 my $class = shift;
558} 559}
559 560
560=item $pid = $proc->pid 561=item $pid = $proc->pid
561 562
562Returns the process id of the process I<iff it is a direct child of the 563Returns the process id of the process I<iff it is a direct child of the
563process> running AnyEvent::Fork, and C<undef> otherwise. 564process running AnyEvent::Fork>, and C<undef> otherwise.
564 565
565Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and 566Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and
566L<AnyEvent::Fork::Template> are direct children, and you are responsible 567L<AnyEvent::Fork::Template> are direct children, and you are responsible
567to clean up their zombies when they die. 568to clean up their zombies when they die.
568 569
569All other processes are not direct children, and will be cleaned up by 570All other processes are not direct children, and will be cleaned up by
570AnyEvent::Fork. 571AnyEvent::Fork itself.
571 572
572=cut 573=cut
573 574
574sub pid { 575sub pid {
575 $_[0][0] 576 $_[0][0]
586 587
587The code will usually be executed after this call returns, and there is no 588The code will usually be executed after this call returns, and there is no
588way to pass anything back to the calling process. Any evaluation errors 589way to pass anything back to the calling process. Any evaluation errors
589will be reported to stderr and cause the process to exit. 590will be reported to stderr and cause the process to exit.
590 591
591If you want to execute some code to take over the process (see the 592If you want to execute some code (that isn't in a module) to take over the
592"fork+exec" example in the SYNOPSIS), you should compile a function via 593process, you should compile a function via C<eval> first, and then call
593C<eval> first, and then call it via C<run>. This also gives you access to 594it via C<run>. This also gives you access to any arguments passed via the
594any arguments passed via the C<send_xxx> methods, such as file handles. 595C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as
596a faster fork+exec> example to see it in action.
595 597
596Returns the process object for easy chaining of method calls. 598Returns the process object for easy chaining of method calls.
597 599
598=cut 600=cut
599 601
625=item $proc = $proc->send_fh ($handle, ...) 627=item $proc = $proc->send_fh ($handle, ...)
626 628
627Send one or more file handles (I<not> file descriptors) to the process, 629Send one or more file handles (I<not> file descriptors) to the process,
628to prepare a call to C<run>. 630to prepare a call to C<run>.
629 631
630The process object keeps a reference to the handles until this is done, 632The process object keeps a reference to the handles until they have
631so you must not explicitly close the handles. This is most easily 633been passed over to the process, so you must not explicitly close the
632accomplished by simply not storing the file handles anywhere after passing 634handles. This is most easily accomplished by simply not storing the file
633them to this method. 635handles anywhere after passing them to this method - when AnyEvent::Fork
636is finished using them, perl will automatically close them.
634 637
635Returns the process object for easy chaining of method calls. 638Returns the process object for easy chaining of method calls.
636 639
637Example: pass a file handle to a process, and release it without 640Example: pass a file handle to a process, and release it without
638closing. It will be closed automatically when it is no longer used. 641closing. It will be closed automatically when it is no longer used.
654} 657}
655 658
656=item $proc = $proc->send_arg ($string, ...) 659=item $proc = $proc->send_arg ($string, ...)
657 660
658Send one or more argument strings to the process, to prepare a call to 661Send one or more argument strings to the process, to prepare a call to
659C<run>. The strings can be any octet string. 662C<run>. The strings can be any octet strings.
660 663
661The protocol is optimised to pass a moderate number of relatively short 664The protocol is optimised to pass a moderate number of relatively short
662strings - while you can pass up to 4GB of data in one go, this is more 665strings - while you can pass up to 4GB of data in one go, this is more
663meant to pass some ID information or other startup info, not big chunks of 666meant to pass some ID information or other startup info, not big chunks of
664data. 667data.
680Enter the function specified by the function name in C<$func> in the 683Enter the function specified by the function name in C<$func> in the
681process. The function is called with the communication socket as first 684process. The function is called with the communication socket as first
682argument, followed by all file handles and string arguments sent earlier 685argument, followed by all file handles and string arguments sent earlier
683via C<send_fh> and C<send_arg> methods, in the order they were called. 686via C<send_fh> and C<send_arg> methods, in the order they were called.
684 687
688The process object becomes unusable on return from this function - any
689further method calls result in undefined behaviour.
690
685The function name should be fully qualified, but if it isn't, it will be 691The function name should be fully qualified, but if it isn't, it will be
686looked up in the main package. 692looked up in the C<main> package.
687 693
688If the called function returns, doesn't exist, or any error occurs, the 694If the called function returns, doesn't exist, or any error occurs, the
689process exits. 695process exits.
690 696
691Preparing the process is done in the background - when all commands have 697Preparing the process is done in the background - when all commands have
692been sent, the callback is invoked with the local communications socket 698been sent, the callback is invoked with the local communications socket
693as argument. At this point you can start using the socket in any way you 699as argument. At this point you can start using the socket in any way you
694like. 700like.
695
696The process object becomes unusable on return from this function - any
697further method calls result in undefined behaviour.
698 701
699If the communication socket isn't used, it should be closed on both sides, 702If the communication socket isn't used, it should be closed on both sides,
700to save on kernel memory. 703to save on kernel memory.
701 704
702The socket is non-blocking in the parent, and blocking in the newly 705The socket is non-blocking in the parent, and blocking in the newly
777 479 vfork+execs per second, using AnyEvent::Fork->new_exec 780 479 vfork+execs per second, using AnyEvent::Fork->new_exec
778 781
779So how can C<< AnyEvent->new >> be faster than a standard fork, even 782So how can C<< AnyEvent->new >> be faster than a standard fork, even
780though it uses the same operations, but adds a lot of overhead? 783though it uses the same operations, but adds a lot of overhead?
781 784
782The difference is simply the process size: forking the 6MB process takes 785The difference is simply the process size: forking the 5MB process takes
783so much longer than forking the 2.5MB template process that the overhead 786so much longer than forking the 2.5MB template process that the extra
784introduced is canceled out. 787overhead introduced is canceled out.
785 788
786If the benchmark process grows, the normal fork becomes even slower: 789If the benchmark process grows, the normal fork becomes even slower:
787 790
788 1340 new processes, manual fork in a 20MB process 791 1340 new processes, manual fork of a 20MB process
789 731 new processes, manual fork in a 200MB process 792 731 new processes, manual fork of a 200MB process
790 235 new processes, manual fork in a 2000MB process 793 235 new processes, manual fork of a 2000MB process
791 794
792What that means (to me) is that I can use this module without having a 795What that means (to me) is that I can use this module without having a bad
793very bad conscience because of the extra overhead required to start new 796conscience because of the extra overhead required to start new processes.
794processes.
795 797
796=head1 TYPICAL PROBLEMS 798=head1 TYPICAL PROBLEMS
797 799
798This section lists typical problems that remain. I hope by recognising 800This section lists typical problems that remain. I hope by recognising
799them, most can be avoided. 801them, most can be avoided.
800 802
801=over 4 803=over 4
802 804
803=item "leaked" file descriptors for exec'ed processes 805=item leaked file descriptors for exec'ed processes
804 806
805POSIX systems inherit file descriptors by default when exec'ing a new 807POSIX systems inherit file descriptors by default when exec'ing a new
806process. While perl itself laudably sets the close-on-exec flags on new 808process. While perl itself laudably sets the close-on-exec flags on new
807file handles, most C libraries don't care, and even if all cared, it's 809file handles, most C libraries don't care, and even if all cared, it's
808often not possible to set the flag in a race-free manner. 810often not possible to set the flag in a race-free manner.
828libraries or the code that leaks those file descriptors. 830libraries or the code that leaks those file descriptors.
829 831
830Fortunately, most of these leaked descriptors do no harm, other than 832Fortunately, most of these leaked descriptors do no harm, other than
831sitting on some resources. 833sitting on some resources.
832 834
833=item "leaked" file descriptors for fork'ed processes 835=item leaked file descriptors for fork'ed processes
834 836
835Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, 837Normally, L<AnyEvent::Fork> does start new processes by exec'ing them,
836which closes file descriptors not marked for being inherited. 838which closes file descriptors not marked for being inherited.
837 839
838However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer 840However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer
847 849
848The solution is to either not load these modules before use'ing 850The solution is to either not load these modules before use'ing
849L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay 851L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay
850initialising them, for example, by calling C<init Gtk2> manually. 852initialising them, for example, by calling C<init Gtk2> manually.
851 853
852=item exit runs destructors 854=item exiting calls object destructors
853 855
854This only applies to users of Lc<AnyEvent::Fork:Early> and 856This only applies to users of L<AnyEvent::Fork:Early> and
855L<AnyEvent::Fork::Template>. 857L<AnyEvent::Fork::Template>, or when initialiasing code creates objects
858that reference external resources.
856 859
857When a process created by AnyEvent::Fork exits, it might do so by calling 860When a process created by AnyEvent::Fork exits, it might do so by calling
858exit, or simply letting perl reach the end of the program. At which point 861exit, or simply letting perl reach the end of the program. At which point
859Perl runs all destructors. 862Perl runs all destructors.
860 863
879to make it so, mostly due to the bloody broken perl that nobody seems to 882to make it so, mostly due to the bloody broken perl that nobody seems to
880care about. The fork emulation is a bad joke - I have yet to see something 883care about. The fork emulation is a bad joke - I have yet to see something
881useful that you can do with it without running into memory corruption 884useful that you can do with it without running into memory corruption
882issues or other braindamage. Hrrrr. 885issues or other braindamage. Hrrrr.
883 886
884Cygwin perl is not supported at the moment, as it should implement fd 887Cygwin perl is not supported at the moment due to some hilarious
885passing, but doesn't, and rolling my own is hard, as cygwin doesn't 888shortcomings of its API - see L<IO::FDPoll> for more details.
886support enough functionality to do it.
887 889
888=head1 SEE ALSO 890=head1 SEE ALSO
889 891
890L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), 892L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter),
891L<AnyEvent::Fork::Template> (to create a process by forking the main 893L<AnyEvent::Fork::Template> (to create a process by forking the main

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines