… | |
… | |
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. |
38 | |
38 | |
39 | If you need some form of RPC, you can either implement it yourself |
39 | If you need some form of RPC, you could use the L<AnyEvent::Fork::RPC> |
40 | in whatever way you like, use some message-passing module such |
40 | companion module, which adds simple RPC/job queueing to a process created |
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
41 | by this module. |
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
42 | |
43 | and so on. |
43 | Or you can implement it yourself in whatever way you like, use some |
|
|
44 | message-passing module such as L<AnyEvent::MP>, some pipe such as |
|
|
45 | L<AnyEvent::ZeroMQ>, use L<AnyEvent::Handle> on both sides to send |
|
|
46 | e.g. JSON or Storable messages, and so on. |
44 | |
47 | |
45 | =head2 COMPARISON TO OTHER MODULES |
48 | =head2 COMPARISON TO OTHER MODULES |
46 | |
49 | |
47 | There is an abundance of modules on CPAN that do "something fork", such as |
50 | There is an abundance of modules on CPAN that do "something fork", such as |
48 | L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker> |
51 | L<Parallel::ForkManager>, L<AnyEvent::ForkManager>, L<AnyEvent::Worker> |
… | |
… | |
221 | } |
224 | } |
222 | } |
225 | } |
223 | |
226 | |
224 | =head2 use AnyEvent::Fork as a faster fork+exec |
227 | =head2 use AnyEvent::Fork as a faster fork+exec |
225 | |
228 | |
226 | This runs C</bin/echo hi>, with stdandard output redirected to /tmp/log |
229 | This runs C</bin/echo hi>, with standard output redirected to F</tmp/log> |
227 | and standard error redirected to the communications socket. It is usually |
230 | and standard error redirected to the communications socket. It is usually |
228 | faster than fork+exec, but still lets you prepare the environment. |
231 | faster than fork+exec, but still lets you prepare the environment. |
229 | |
232 | |
230 | open my $output, ">/tmp/log" or die "$!"; |
233 | open my $output, ">/tmp/log" or die "$!"; |
231 | |
234 | |
… | |
… | |
251 | |
254 | |
252 | =head1 CONCEPTS |
255 | =head1 CONCEPTS |
253 | |
256 | |
254 | This module can create new processes either by executing a new perl |
257 | This module can create new processes either by executing a new perl |
255 | process, or by forking from an existing "template" process. |
258 | process, or by forking from an existing "template" process. |
|
|
259 | |
|
|
260 | All these processes are called "child processes" (whether they are direct |
|
|
261 | children or not), while the process that manages them is called the |
|
|
262 | "parent process". |
256 | |
263 | |
257 | Each such process comes with its own file handle that can be used to |
264 | Each such process comes with its own file handle that can be used to |
258 | communicate with it (it's actually a socket - one end in the new process, |
265 | communicate with it (it's actually a socket - one end in the new process, |
259 | one end in the main process), and among the things you can do in it are |
266 | one end in the main process), and among the things you can do in it are |
260 | load modules, fork new processes, send file handles to it, and execute |
267 | load modules, fork new processes, send file handles to it, and execute |
… | |
… | |
370 | use AnyEvent; |
377 | use AnyEvent; |
371 | use AnyEvent::Util (); |
378 | use AnyEvent::Util (); |
372 | |
379 | |
373 | use IO::FDPass; |
380 | use IO::FDPass; |
374 | |
381 | |
375 | our $VERSION = 0.6; |
382 | our $VERSION = 0.7; |
376 | |
|
|
377 | =over 4 |
|
|
378 | |
|
|
379 | =back |
|
|
380 | |
|
|
381 | =cut |
|
|
382 | |
383 | |
383 | # the early fork template process |
384 | # the early fork template process |
384 | our $EARLY; |
385 | our $EARLY; |
385 | |
386 | |
386 | # the empty template process |
387 | # the empty template process |
… | |
… | |
598 | $_[0][PID] |
599 | $_[0][PID] |
599 | } |
600 | } |
600 | |
601 | |
601 | =item $proc = $proc->eval ($perlcode, @args) |
602 | =item $proc = $proc->eval ($perlcode, @args) |
602 | |
603 | |
603 | Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to |
604 | Evaluates the given C<$perlcode> as ... Perl code, while setting C<@_> to |
604 | the strings specified by C<@args>, in the "main" package. |
605 | the strings specified by C<@args>, in the "main" package. |
605 | |
606 | |
606 | This call is meant to do any custom initialisation that might be required |
607 | This call is meant to do any custom initialisation that might be required |
607 | (for example, the C<require> method uses it). It's not supposed to be used |
608 | (for example, the C<require> method uses it). It's not supposed to be used |
608 | to completely take over the process, use C<run> for that. |
609 | to completely take over the process, use C<run> for that. |
… | |
… | |
804 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
805 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
805 | though it uses the same operations, but adds a lot of overhead? |
806 | though it uses the same operations, but adds a lot of overhead? |
806 | |
807 | |
807 | The difference is simply the process size: forking the 5MB process takes |
808 | The difference is simply the process size: forking the 5MB process takes |
808 | so much longer than forking the 2.5MB template process that the extra |
809 | so much longer than forking the 2.5MB template process that the extra |
809 | overhead introduced is canceled out. |
810 | overhead is canceled out. |
810 | |
811 | |
811 | If the benchmark process grows, the normal fork becomes even slower: |
812 | If the benchmark process grows, the normal fork becomes even slower: |
812 | |
813 | |
813 | 1340 new processes, manual fork of a 20MB process |
814 | 1340 new processes, manual fork of a 20MB process |
814 | 731 new processes, manual fork of a 200MB process |
815 | 731 new processes, manual fork of a 200MB process |
… | |
… | |
874 | initialising them, for example, by calling C<init Gtk2> manually. |
875 | initialising them, for example, by calling C<init Gtk2> manually. |
875 | |
876 | |
876 | =item exiting calls object destructors |
877 | =item exiting calls object destructors |
877 | |
878 | |
878 | This only applies to users of L<AnyEvent::Fork:Early> and |
879 | This only applies to users of L<AnyEvent::Fork:Early> and |
879 | L<AnyEvent::Fork::Template>, or when initialiasing code creates objects |
880 | L<AnyEvent::Fork::Template>, or when initialising code creates objects |
880 | that reference external resources. |
881 | that reference external resources. |
881 | |
882 | |
882 | When a process created by AnyEvent::Fork exits, it might do so by calling |
883 | When a process created by AnyEvent::Fork exits, it might do so by calling |
883 | exit, or simply letting perl reach the end of the program. At which point |
884 | exit, or simply letting perl reach the end of the program. At which point |
884 | Perl runs all destructors. |
885 | Perl runs all destructors. |
… | |
… | |
909 | Cygwin perl is not supported at the moment due to some hilarious |
910 | Cygwin perl is not supported at the moment due to some hilarious |
910 | shortcomings of its API - see L<IO::FDPoll> for more details. |
911 | shortcomings of its API - see L<IO::FDPoll> for more details. |
911 | |
912 | |
912 | =head1 SEE ALSO |
913 | =head1 SEE ALSO |
913 | |
914 | |
914 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
915 | L<AnyEvent::Fork::Early>, to avoid executing a perl interpreter at all |
|
|
916 | (part of this distribution). |
|
|
917 | |
915 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
918 | L<AnyEvent::Fork::Template>, to create a process by forking the main |
916 | program at a convenient time). |
919 | program at a convenient time (part of this distribution). |
917 | |
920 | |
918 | =head1 AUTHOR |
921 | L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). |
|
|
922 | |
|
|
923 | =head1 AUTHOR AND CONTACT INFORMATION |
919 | |
924 | |
920 | Marc Lehmann <schmorp@schmorp.de> |
925 | Marc Lehmann <schmorp@schmorp.de> |
921 | http://home.schmorp.de/ |
926 | http://software.schmorp.de/pkg/AnyEvent-Fork |
922 | |
927 | |
923 | =cut |
928 | =cut |
924 | |
929 | |
925 | 1 |
930 | 1 |
926 | |
931 | |