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.38 by root, Sat Apr 6 20:07:30 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 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines