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.56 by root, Sun Apr 28 13:47:52 2013 UTC vs.
Revision 1.62 by root, Mon Nov 25 20:21:09 2013 UTC

256 256
257 my $stderr = $cv->recv; 257 my $stderr = $cv->recv;
258 258
259=head2 For stingy users: put the worker code into a C<DATA> section. 259=head2 For stingy users: put the worker code into a C<DATA> section.
260 260
261When you want to be stingy with files, you cna put your code into the 261When you want to be stingy with files, you can put your code into the
262C<DATA> section of your module (or program): 262C<DATA> section of your module (or program):
263 263
264 use AnyEvent::Fork; 264 use AnyEvent::Fork;
265 265
266 AnyEvent::Fork 266 AnyEvent::Fork
276 276
277=head2 For stingy standalone programs: do not rely on external files at 277=head2 For stingy standalone programs: do not rely on external files at
278all. 278all.
279 279
280For single-file scripts it can be inconvenient to rely on external 280For single-file scripts it can be inconvenient to rely on external
281files - even when using < C<DATA> section, you still need to C<exec> 281files - even when using a C<DATA> section, you still need to C<exec> an
282an external perl interpreter, which might not be available when using 282external perl interpreter, which might not be available when using
283L<App::Staticperl>, L<Urlader> or L<PAR::Packer> for example. 283L<App::Staticperl>, L<Urlader> or L<PAR::Packer> for example.
284 284
285Two modules help here - L<AnyEvent::Fork::Early> forks a template process 285Two modules help here - L<AnyEvent::Fork::Early> forks a template process
286for all further calls to C<new_exec>, and L<AnyEvent::Fork::Template> 286for all further calls to C<new_exec>, and L<AnyEvent::Fork::Template>
287forks the main program as a template process. 287forks the main program as a template process.
304 my ($fh, @args) = @_; 304 my ($fh, @args) = @_;
305 ... 305 ...
306 } 306 }
307 307
308 # now preserve everything so far as AnyEvent::Fork object 308 # now preserve everything so far as AnyEvent::Fork object
309 # in ยงTEMPLATE. 309 # in $TEMPLATE.
310 use AnyEvent::Fork::Template; 310 use AnyEvent::Fork::Template;
311 311
312 # do not put code outside of BEGIN blocks until here 312 # do not put code outside of BEGIN blocks until here
313 313
314 # now use the $TEMPLATE process in any way you like 314 # now use the $TEMPLATE process in any way you like
450use AnyEvent; 450use AnyEvent;
451use AnyEvent::Util (); 451use AnyEvent::Util ();
452 452
453use IO::FDPass; 453use IO::FDPass;
454 454
455our $VERSION = 1.1; 455our $VERSION = 1.2;
456 456
457# the early fork template process 457# the early fork template process
458our $EARLY; 458our $EARLY;
459 459
460# the empty template process 460# the empty template process
605 605
606You should use C<new> whenever possible, except when having a template 606You should use C<new> whenever possible, except when having a template
607process around is unacceptable. 607process around is unacceptable.
608 608
609The path to the perl interpreter is divined using various methods - first 609The path to the perl interpreter is divined using various methods - first
610C<$^X> is investigated to see if the path ends with something that sounds 610C<$^X> is investigated to see if the path ends with something that looks
611as if it were the perl interpreter. Failing this, the module falls back to 611as if it were the perl interpreter. Failing this, the module falls back to
612using C<$Config::Config{perlpath}>. 612using C<$Config::Config{perlpath}>.
613 613
614The path to perl can also be overriden by setting the global variable 614The path to perl can also be overriden by setting the global variable
615C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent 615C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent
625 return $EARLY->fork 625 return $EARLY->fork
626 if $EARLY; 626 if $EARLY;
627 627
628 unless (defined $PERL) { 628 unless (defined $PERL) {
629 # first find path of perl 629 # first find path of perl
630 my $perl = $; 630 my $perl = $^X;
631 631
632 # first we try $^X, but the path must be absolute (always on win32), and end in sth. 632 # first we try $^X, but the path must be absolute (always on win32), and end in sth.
633 # that looks like perl. this obviously only works for posix and win32 633 # that looks like perl. this obviously only works for posix and win32
634 unless ( 634 unless (
635 ($^O eq "MSWin32" || $perl =~ m%^/%) 635 ($^O eq "MSWin32" || $perl =~ m%^/%)
668} 668}
669 669
670=item $pid = $proc->pid 670=item $pid = $proc->pid
671 671
672Returns the process id of the process I<iff it is a direct child of the 672Returns the process id of the process I<iff it is a direct child of the
673process running AnyEvent::Fork>, and C<undef> otherwise. 673process running AnyEvent::Fork>, and C<undef> otherwise. As a general
674rule (that you cannot rely upon), processes created via C<new_exec>,
675L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template> are direct
676children, while all other processes are not.
674 677
675Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and 678Or in other words, you do not normally have to take care of zombies for
676L<AnyEvent::Fork::Template> are direct children, and you are responsible 679processes created via C<new>, but when in doubt, or zombies are a problem,
677to clean up their zombies when they die. 680you need to check whether a process is a diretc child by calling this
678 681method, and possibly creating a child watcher or reap it manually.
679All other processes are not direct children, and will be cleaned up by
680AnyEvent::Fork itself.
681 682
682=cut 683=cut
683 684
684sub pid { 685sub pid {
685 $_[0][PID] 686 $_[0][PID]
816 817
817Even if not used otherwise, the socket can be a good indicator for the 818Even if not used otherwise, the socket can be a good indicator for the
818existence of the process - if the other process exits, you get a readable 819existence of the process - if the other process exits, you get a readable
819event on it, because exiting the process closes the socket (if it didn't 820event on it, because exiting the process closes the socket (if it didn't
820create any children using fork). 821create any children using fork).
822
823=over 4
824
825=item Compatibility to L<AnyEvent::Fork::Remote>
826
827If you want to write code that works with both this module and
828L<AnyEvent::Fork::Remote>, you need to write your code so that it assumes
829there are two file handles for communications, which might not be unix
830domain sockets. The C<run> function should start like this:
831
832 sub run {
833 my ($rfh, @args) = @_; # @args is your normal arguments
834 my $wfh = fileno $rfh ? $rfh : *STDOUT;
835
836 # now use $rfh for reading and $wfh for writing
837 }
838
839This checks whether the passed file handle is, in fact, the process
840C<STDIN> handle. If it is, then the function was invoked visa
841L<AnyEvent::Fork::Remote>, so STDIN should be used for reading and
842C<STDOUT> should be used for writing.
843
844In all other cases, the function was called via this module, and there is
845only one file handle that should be sued for reading and writing.
846
847=back
821 848
822Example: create a template for a process pool, pass a few strings, some 849Example: create a template for a process pool, pass a few strings, some
823file handles, then fork, pass one more string, and run some code. 850file handles, then fork, pass one more string, and run some code.
824 851
825 my $pool = AnyEvent::Fork 852 my $pool = AnyEvent::Fork
873the communications socket. 900the communications socket.
874 901
875The process object becomes unusable on return from this function - any 902The process object becomes unusable on return from this function - any
876further method calls result in undefined behaviour. 903further method calls result in undefined behaviour.
877 904
878The point of this method is to give you a file handle thta you cna pass 905The point of this method is to give you a file handle that you can pass
879to another process. In that other process, you can call C<new_from_fh 906to another process. In that other process, you can call C<new_from_fh
880AnyEvent::Fork> to create a new C<AnyEvent::Fork> object from it, thereby 907AnyEvent::Fork $fh> to create a new C<AnyEvent::Fork> object from it,
881effectively passing a fork object to another process. 908thereby effectively passing a fork object to another process.
882 909
883=cut 910=cut
884 911
885sub to_fh { 912sub to_fh {
886 my ($self, $cb) = @_; 913 my ($self, $cb) = @_;
1061(part of this distribution). 1088(part of this distribution).
1062 1089
1063L<AnyEvent::Fork::Template>, to create a process by forking the main 1090L<AnyEvent::Fork::Template>, to create a process by forking the main
1064program at a convenient time (part of this distribution). 1091program at a convenient time (part of this distribution).
1065 1092
1093L<AnyEvent::Fork::Remote>, for another way to create processes that is
1094mostly compatible to this module and modules building on top of it, but
1095works better with remote processes.
1096
1066L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). 1097L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN).
1067 1098
1068L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN). 1099L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN).
1069 1100
1070=head1 AUTHOR AND CONTACT INFORMATION 1101=head1 AUTHOR AND CONTACT INFORMATION

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines