… | |
… | |
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::worker might look like this |
157 | C<MyModule> might look like this: |
|
|
158 | |
|
|
159 | package MyModule; |
|
|
160 | |
158 | sub MyModule::worker { |
161 | sub worker { |
159 | my ($slave_filehandle) = @_; |
162 | my ($slave_filehandle) = @_; |
160 | |
163 | |
161 | # now $slave_filehandle is connected to the $master_filehandle |
164 | # now $slave_filehandle is connected to the $master_filehandle |
162 | # in the original prorcess. have fun! |
165 | # in the original prorcess. have fun! |
163 | } |
166 | } |
… | |
… | |
182 | } |
185 | } |
183 | |
186 | |
184 | # now do other things - maybe use the filehandle provided by run |
187 | # now do other things - maybe use the filehandle provided by run |
185 | # to wait for the processes to die. or whatever. |
188 | # to wait for the processes to die. or whatever. |
186 | |
189 | |
187 | # My::Server::run might look like this |
190 | C<My::Server> might look like this: |
188 | sub My::Server::run { |
191 | |
|
|
192 | package My::Server; |
|
|
193 | |
|
|
194 | sub run { |
189 | my ($slave, $listener, $id) = @_; |
195 | my ($slave, $listener, $id) = @_; |
190 | |
196 | |
191 | close $slave; # we do not use the socket, so close it to save resources |
197 | close $slave; # we do not use the socket, so close it to save resources |
192 | |
198 | |
193 | # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
199 | # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
… | |
… | |
197 | } |
203 | } |
198 | } |
204 | } |
199 | |
205 | |
200 | =head2 use AnyEvent::Fork as a faster fork+exec |
206 | =head2 use AnyEvent::Fork as a faster fork+exec |
201 | |
207 | |
202 | 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 |
203 | the communications socket. It is usually faster than fork+exec, but still |
209 | and standard error redirected to the communications socket. It is usually |
204 | let's you prepare the environment. |
210 | faster than fork+exec, but still lets you prepare the environment. |
205 | |
211 | |
206 | open my $output, ">/tmp/log" or die "$!"; |
212 | open my $output, ">/tmp/log" or die "$!"; |
207 | |
213 | |
208 | AnyEvent::Fork |
214 | AnyEvent::Fork |
209 | ->new |
215 | ->new |
… | |
… | |
309 | my ($fork_fh) = @_; |
315 | my ($fork_fh) = @_; |
310 | }); |
316 | }); |
311 | |
317 | |
312 | =back |
318 | =back |
313 | |
319 | |
314 | =head1 FUNCTIONS |
320 | =head1 THE C<AnyEvent::Fork> CLASS |
|
|
321 | |
|
|
322 | This module exports nothing, and only implements a single class - |
|
|
323 | C<AnyEvent::Fork>. |
|
|
324 | |
|
|
325 | There are two class constructors that both create new processes - C<new> |
|
|
326 | and C<new_exec>. The C<fork> method creates a new process by forking an |
|
|
327 | existing one and could be considered a third constructor. |
|
|
328 | |
|
|
329 | Most of the remaining methods deal with preparing the new process, by |
|
|
330 | loading code, evaluating code and sending data to the new process. They |
|
|
331 | usually return the process object, so you can chain method calls. |
|
|
332 | |
|
|
333 | If a process object is destroyed before calling its C<run> method, then |
|
|
334 | the process simply exits. After C<run> is called, all responsibility is |
|
|
335 | passed to the specified function. |
|
|
336 | |
|
|
337 | As long as there is any outstanding work to be done, process objects |
|
|
338 | resist being destroyed, so there is no reason to store them unless you |
|
|
339 | need them later - configure and forget works just fine. |
315 | |
340 | |
316 | =over 4 |
341 | =over 4 |
317 | |
342 | |
318 | =cut |
343 | =cut |
319 | |
344 | |
… | |
… | |
329 | use IO::FDPass; |
354 | use IO::FDPass; |
330 | |
355 | |
331 | our $VERSION = 0.5; |
356 | our $VERSION = 0.5; |
332 | |
357 | |
333 | our $PERL; # the path to the perl interpreter, deduces with various forms of magic |
358 | our $PERL; # the path to the perl interpreter, deduces with various forms of magic |
334 | |
|
|
335 | =item my $pool = new AnyEvent::Fork key => value... |
|
|
336 | |
|
|
337 | Create a new process pool. The following named parameters are supported: |
|
|
338 | |
359 | |
339 | =over 4 |
360 | =over 4 |
340 | |
361 | |
341 | =back |
362 | =back |
342 | |
363 | |
… | |
… | |
438 | Create a new "empty" perl interpreter process and returns its process |
459 | Create a new "empty" perl interpreter process and returns its process |
439 | object for further manipulation. |
460 | object for further manipulation. |
440 | |
461 | |
441 | The new process is forked from a template process that is kept around |
462 | The new process is forked from a template process that is kept around |
442 | for this purpose. When it doesn't exist yet, it is created by a call to |
463 | for this purpose. When it doesn't exist yet, it is created by a call to |
443 | C<new_exec> and kept around for future calls. |
464 | C<new_exec> first and then stays around for future calls. |
444 | |
|
|
445 | When the process object is destroyed, it will release the file handle |
|
|
446 | that connects it with the new process. When the new process has not yet |
|
|
447 | called C<run>, then the process will exit. Otherwise, what happens depends |
|
|
448 | entirely on the code that is executed. |
|
|
449 | |
465 | |
450 | =cut |
466 | =cut |
451 | |
467 | |
452 | sub new { |
468 | sub new { |
453 | my $class = shift; |
469 | my $class = shift; |
… | |
… | |
543 | } |
559 | } |
544 | |
560 | |
545 | =item $pid = $proc->pid |
561 | =item $pid = $proc->pid |
546 | |
562 | |
547 | 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 |
548 | process> running AnyEvent::Fork, and C<undef> otherwise. |
564 | process running AnyEvent::Fork>, and C<undef> otherwise. |
549 | |
565 | |
550 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
566 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
551 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
567 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
552 | to clean up their zombies when they die. |
568 | to clean up their zombies when they die. |
553 | |
569 | |
554 | All other processes are not direct children, and will be cleaned up by |
570 | All other processes are not direct children, and will be cleaned up by |
555 | AnyEvent::Fork. |
571 | AnyEvent::Fork itself. |
556 | |
572 | |
557 | =cut |
573 | =cut |
558 | |
574 | |
559 | sub pid { |
575 | sub pid { |
560 | $_[0][0] |
576 | $_[0][0] |
… | |
… | |
571 | |
587 | |
572 | 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 |
573 | 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 |
574 | will be reported to stderr and cause the process to exit. |
590 | will be reported to stderr and cause the process to exit. |
575 | |
591 | |
576 | 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 |
577 | "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 |
578 | 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 |
579 | 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. |
580 | |
597 | |
581 | Returns the process object for easy chaining of method calls. |
598 | Returns the process object for easy chaining of method calls. |
582 | |
599 | |
583 | =cut |
600 | =cut |
584 | |
601 | |