… | |
… | |
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 |
… | |
… | |
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 | } |
… | |
… | |
717 | it via C<run>. This also gives you access to any arguments passed via the |
719 | 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 |
720 | 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. |
721 | a faster fork+exec> example to see it in action. |
720 | |
722 | |
721 | Returns the process object for easy chaining of method calls. |
723 | Returns the process object for easy chaining of method calls. |
|
|
724 | |
|
|
725 | It's common to want to call an iniitalisation function with some |
|
|
726 | arguments. Make sure you actually pass C<@_> to that function (for example |
|
|
727 | by using C<&name> syntax), and do not just specify a function name: |
|
|
728 | |
|
|
729 | $proc->eval ('&MyModule::init', $string1, $string2); |
722 | |
730 | |
723 | =cut |
731 | =cut |
724 | |
732 | |
725 | sub eval { |
733 | sub eval { |
726 | my ($self, $code, @args) = @_; |
734 | my ($self, $code, @args) = @_; |