… | |
… | |
152 | |
152 | |
153 | # now $master_filehandle is connected to the |
153 | # now $master_filehandle is connected to the |
154 | # $slave_filehandle in the new process. |
154 | # $slave_filehandle in the new process. |
155 | }); |
155 | }); |
156 | |
156 | |
157 | MyModule might look like this: |
157 | C<MyModule> might look like this: |
158 | |
158 | |
159 | package MyModule; |
159 | package MyModule; |
160 | |
160 | |
161 | sub worker { |
161 | sub worker { |
162 | my ($slave_filehandle) = @_; |
162 | my ($slave_filehandle) = @_; |
… | |
… | |
185 | } |
185 | } |
186 | |
186 | |
187 | # now do other things - maybe use the filehandle provided by run |
187 | # now do other things - maybe use the filehandle provided by run |
188 | # to wait for the processes to die. or whatever. |
188 | # to wait for the processes to die. or whatever. |
189 | |
189 | |
190 | My::Server might look like this: |
190 | C<My::Server> might look like this: |
191 | |
191 | |
192 | package My::Server; |
192 | package My::Server; |
193 | |
193 | |
194 | sub run { |
194 | sub run { |
195 | my ($slave, $listener, $id) = @_; |
195 | my ($slave, $listener, $id) = @_; |
… | |
… | |
203 | } |
203 | } |
204 | } |
204 | } |
205 | |
205 | |
206 | =head2 use AnyEvent::Fork as a faster fork+exec |
206 | =head2 use AnyEvent::Fork as a faster fork+exec |
207 | |
207 | |
208 | This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to |
208 | This runs C</bin/echo hi>, with stdandard output redirected to /tmp/log |
209 | the communications socket. It is usually faster than fork+exec, but still |
209 | and standard error redirected to the communications socket. It is usually |
210 | let's you prepare the environment. |
210 | faster than fork+exec, but still lets you prepare the environment. |
211 | |
211 | |
212 | open my $output, ">/tmp/log" or die "$!"; |
212 | open my $output, ">/tmp/log" or die "$!"; |
213 | |
213 | |
214 | AnyEvent::Fork |
214 | AnyEvent::Fork |
215 | ->new |
215 | ->new |
… | |
… | |
559 | } |
559 | } |
560 | |
560 | |
561 | =item $pid = $proc->pid |
561 | =item $pid = $proc->pid |
562 | |
562 | |
563 | Returns the process id of the process I<iff it is a direct child of the |
563 | Returns the process id of the process I<iff it is a direct child of the |
564 | process> running AnyEvent::Fork, and C<undef> otherwise. |
564 | process running AnyEvent::Fork>, and C<undef> otherwise. |
565 | |
565 | |
566 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
566 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
567 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
567 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
568 | to clean up their zombies when they die. |
568 | to clean up their zombies when they die. |
569 | |
569 | |
… | |
… | |
587 | |
587 | |
588 | The code will usually be executed after this call returns, and there is no |
588 | The code will usually be executed after this call returns, and there is no |
589 | way to pass anything back to the calling process. Any evaluation errors |
589 | way to pass anything back to the calling process. Any evaluation errors |
590 | will be reported to stderr and cause the process to exit. |
590 | will be reported to stderr and cause the process to exit. |
591 | |
591 | |
592 | If you want to execute some code to take over the process (see the |
592 | If you want to execute some code (that isn't in a module) to take over the |
593 | "fork+exec" example in the SYNOPSIS), you should compile a function via |
593 | process, you should compile a function via C<eval> first, and then call |
594 | C<eval> first, and then call it via C<run>. This also gives you access to |
594 | it via C<run>. This also gives you access to any arguments passed via the |
595 | any arguments passed via the C<send_xxx> methods, such as file handles. |
595 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
|
|
596 | a faster fork+exec> example to see it in action. |
596 | |
597 | |
597 | Returns the process object for easy chaining of method calls. |
598 | Returns the process object for easy chaining of method calls. |
598 | |
599 | |
599 | =cut |
600 | =cut |
600 | |
601 | |