… | |
… | |
178 | |
178 | |
179 | =back |
179 | =back |
180 | |
180 | |
181 | =head1 EXAMPLES |
181 | =head1 EXAMPLES |
182 | |
182 | |
|
|
183 | This is where the wall of text ends and code speaks. |
|
|
184 | |
183 | =head2 Create a single new process, tell it to run your worker function. |
185 | =head2 Create a single new process, tell it to run your worker function. |
184 | |
186 | |
185 | AnyEvent::Fork |
187 | AnyEvent::Fork |
186 | ->new |
188 | ->new |
187 | ->require ("MyModule") |
189 | ->require ("MyModule") |
… | |
… | |
198 | |
200 | |
199 | sub worker { |
201 | sub worker { |
200 | my ($slave_filehandle) = @_; |
202 | my ($slave_filehandle) = @_; |
201 | |
203 | |
202 | # now $slave_filehandle is connected to the $master_filehandle |
204 | # now $slave_filehandle is connected to the $master_filehandle |
203 | # in the original prorcess. have fun! |
205 | # in the original process. have fun! |
204 | } |
206 | } |
205 | |
207 | |
206 | =head2 Create a pool of server processes all accepting on the same socket. |
208 | =head2 Create a pool of server processes all accepting on the same socket. |
207 | |
209 | |
208 | # create listener socket |
210 | # create listener socket |
… | |
… | |
463 | use AnyEvent; |
465 | use AnyEvent; |
464 | use AnyEvent::Util (); |
466 | use AnyEvent::Util (); |
465 | |
467 | |
466 | use IO::FDPass; |
468 | use IO::FDPass; |
467 | |
469 | |
468 | our $VERSION = 1.2; |
470 | our $VERSION = 1.32; |
469 | |
471 | |
470 | # the early fork template process |
472 | # the early fork template process |
471 | our $EARLY; |
473 | our $EARLY; |
472 | |
474 | |
473 | # the empty template process |
475 | # the empty template process |
… | |
… | |
554 | |
556 | |
555 | if ($pid eq 0) { |
557 | if ($pid eq 0) { |
556 | require AnyEvent::Fork::Serve; |
558 | require AnyEvent::Fork::Serve; |
557 | $AnyEvent::Fork::Serve::OWNER = $parent; |
559 | $AnyEvent::Fork::Serve::OWNER = $parent; |
558 | close $fh; |
560 | close $fh; |
559 | $0 = "$_[1] of $parent"; |
561 | $0 = "$parent AnyEvent::Fork/exec"; |
560 | AnyEvent::Fork::Serve::serve ($slave); |
562 | AnyEvent::Fork::Serve::serve ($slave); |
561 | exit 0; |
563 | exit 0; |
562 | } elsif (!$pid) { |
564 | } elsif (!$pid) { |
563 | die "AnyEvent::Fork::Early/Template: unable to fork template process: $!"; |
565 | die "AnyEvent::Fork::Early/Template: unable to fork template process: $!"; |
564 | } |
566 | } |
… | |
… | |
622 | The path to the perl interpreter is divined using various methods - first |
624 | The path to the perl interpreter is divined using various methods - first |
623 | C<$^X> is investigated to see if the path ends with something that looks |
625 | C<$^X> is investigated to see if the path ends with something that looks |
624 | as if it were the perl interpreter. Failing this, the module falls back to |
626 | as if it were the perl interpreter. Failing this, the module falls back to |
625 | using C<$Config::Config{perlpath}>. |
627 | using C<$Config::Config{perlpath}>. |
626 | |
628 | |
627 | The path to perl can also be overriden by setting the global variable |
629 | The path to perl can also be overridden by setting the global variable |
628 | C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent |
630 | C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent |
629 | invocations. |
631 | invocations. |
630 | |
632 | |
631 | =cut |
633 | =cut |
632 | |
634 | |
… | |
… | |
671 | my %env = %ENV; |
673 | my %env = %ENV; |
672 | $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; |
674 | $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; |
673 | |
675 | |
674 | my $pid = Proc::FastSpawn::spawn ( |
676 | my $pid = Proc::FastSpawn::spawn ( |
675 | $PERL, |
677 | $PERL, |
676 | ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], |
678 | [$PERL, "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], |
677 | [map "$_=$env{$_}", keys %env], |
679 | [map "$_=$env{$_}", keys %env], |
678 | ) or die "unable to spawn AnyEvent::Fork server: $!"; |
680 | ) or die "unable to spawn AnyEvent::Fork server: $!"; |
679 | |
681 | |
680 | $self->_new ($fh, $pid) |
682 | $self->_new ($fh, $pid) |
681 | } |
683 | } |
… | |
… | |
699 | $_[0][PID] |
701 | $_[0][PID] |
700 | } |
702 | } |
701 | |
703 | |
702 | =item $proc = $proc->eval ($perlcode, @args) |
704 | =item $proc = $proc->eval ($perlcode, @args) |
703 | |
705 | |
704 | Evaluates the given C<$perlcode> as ... Perl code, while setting C<@_> to |
706 | Evaluates the given C<$perlcode> as ... Perl code, while setting C<@_> |
705 | the strings specified by C<@args>, in the "main" package. |
707 | to the strings specified by C<@args>, in the "main" package (so you can |
|
|
708 | access the args using C<$_[0]> and so on, but not using implicit C<shit> |
|
|
709 | as the latter works on C<@ARGV>). |
706 | |
710 | |
707 | This call is meant to do any custom initialisation that might be required |
711 | This call is meant to do any custom initialisation that might be required |
708 | (for example, the C<require> method uses it). It's not supposed to be used |
712 | (for example, the C<require> method uses it). It's not supposed to be used |
709 | to completely take over the process, use C<run> for that. |
713 | to completely take over the process, use C<run> for that. |
710 | |
714 | |
… | |
… | |
717 | it via C<run>. This also gives you access to any arguments passed via the |
721 | it via C<run>. This also gives you access to any arguments passed via the |
718 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
722 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
719 | a faster fork+exec> example to see it in action. |
723 | a faster fork+exec> example to see it in action. |
720 | |
724 | |
721 | Returns the process object for easy chaining of method calls. |
725 | Returns the process object for easy chaining of method calls. |
|
|
726 | |
|
|
727 | It's common to want to call an iniitalisation function with some |
|
|
728 | arguments. Make sure you actually pass C<@_> to that function (for example |
|
|
729 | by using C<&name> syntax), and do not just specify a function name: |
|
|
730 | |
|
|
731 | $proc->eval ('&MyModule::init', $string1, $string2); |
722 | |
732 | |
723 | =cut |
733 | =cut |
724 | |
734 | |
725 | sub eval { |
735 | sub eval { |
726 | my ($self, $code, @args) = @_; |
736 | my ($self, $code, @args) = @_; |
… | |
… | |
899 | $self->_cmd (r => $func); |
909 | $self->_cmd (r => $func); |
900 | } |
910 | } |
901 | |
911 | |
902 | =back |
912 | =back |
903 | |
913 | |
|
|
914 | |
|
|
915 | =head2 CHILD PROCESS INTERFACE |
|
|
916 | |
|
|
917 | This module has a limited API for use in child processes. |
|
|
918 | |
|
|
919 | =over 4 |
|
|
920 | |
|
|
921 | =item @args = AnyEvent::Fork::Serve::run_args |
|
|
922 | |
|
|
923 | This function, which only exists before the C<run> method is called, |
|
|
924 | returns the arguments that would be passed to the run function, and clears |
|
|
925 | them. |
|
|
926 | |
|
|
927 | This is mainly useful to get any file handles passed via C<send_fh>, but |
|
|
928 | works for any arguments passed via C<< send_I<xxx> >> methods. |
|
|
929 | |
|
|
930 | =back |
|
|
931 | |
|
|
932 | |
904 | =head2 EXPERIMENTAL METHODS |
933 | =head2 EXPERIMENTAL METHODS |
905 | |
934 | |
906 | These methods might go away completely or change behaviour, at any time. |
935 | These methods might go away completely or change behaviour, at any time. |
907 | |
936 | |
908 | =over 4 |
937 | =over 4 |