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.42 by root, Mon Apr 8 05:44:23 2013 UTC vs.
Revision 1.47 by root, Thu Apr 18 20:17:34 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines