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.44 by root, Thu Apr 18 10:49:59 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
371use AnyEvent::Util (); 374use AnyEvent::Util ();
372 375
373use IO::FDPass; 376use IO::FDPass;
374 377
375our $VERSION = 0.6; 378our $VERSION = 0.6;
376
377=over 4
378
379=back
380
381=cut
382 379
383# the early fork template process 380# the early fork template process
384our $EARLY; 381our $EARLY;
385 382
386# the empty template process 383# the empty template process
598 $_[0][PID] 595 $_[0][PID]
599} 596}
600 597
601=item $proc = $proc->eval ($perlcode, @args) 598=item $proc = $proc->eval ($perlcode, @args)
602 599
603Evaluates the given C<$perlcode> as ... perl code, while setting C<@_> to 600Evaluates the given C<$perlcode> as ... Perl code, while setting C<@_> to
604the strings specified by C<@args>, in the "main" package. 601the strings specified by C<@args>, in the "main" package.
605 602
606This call is meant to do any custom initialisation that might be required 603This 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 604(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. 605to completely take over the process, use C<run> for that.
804So how can C<< AnyEvent->new >> be faster than a standard fork, even 801So how can C<< AnyEvent->new >> be faster than a standard fork, even
805though it uses the same operations, but adds a lot of overhead? 802though it uses the same operations, but adds a lot of overhead?
806 803
807The difference is simply the process size: forking the 5MB process takes 804The difference is simply the process size: forking the 5MB process takes
808so much longer than forking the 2.5MB template process that the extra 805so much longer than forking the 2.5MB template process that the extra
809overhead introduced is canceled out. 806overhead is canceled out.
810 807
811If the benchmark process grows, the normal fork becomes even slower: 808If the benchmark process grows, the normal fork becomes even slower:
812 809
813 1340 new processes, manual fork of a 20MB process 810 1340 new processes, manual fork of a 20MB process
814 731 new processes, manual fork of a 200MB process 811 731 new processes, manual fork of a 200MB process
874initialising them, for example, by calling C<init Gtk2> manually. 871initialising them, for example, by calling C<init Gtk2> manually.
875 872
876=item exiting calls object destructors 873=item exiting calls object destructors
877 874
878This only applies to users of L<AnyEvent::Fork:Early> and 875This only applies to users of L<AnyEvent::Fork:Early> and
879L<AnyEvent::Fork::Template>, or when initialiasing code creates objects 876L<AnyEvent::Fork::Template>, or when initialising code creates objects
880that reference external resources. 877that reference external resources.
881 878
882When a process created by AnyEvent::Fork exits, it might do so by calling 879When 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 880exit, or simply letting perl reach the end of the program. At which point
884Perl runs all destructors. 881Perl runs all destructors.
911 908
912=head1 SEE ALSO 909=head1 SEE ALSO
913 910
914L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), 911L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter),
915L<AnyEvent::Fork::Template> (to create a process by forking the main 912L<AnyEvent::Fork::Template> (to create a process by forking the main
916program at a convenient time). 913program at a convenient time), L<AnyEvent::Fork::RPC> (for simple RPC to
914child processes).
917 915
918=head1 AUTHOR 916=head1 AUTHOR AND CONTACT INFORMATION
919 917
920 Marc Lehmann <schmorp@schmorp.de> 918 Marc Lehmann <schmorp@schmorp.de>
921 http://home.schmorp.de/ 919 http://software.schmorp.de/pkg/AnyEvent-Fork
922 920
923=cut 921=cut
924 922
9251 9231
926 924

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines