… | |
… | |
125 | becomes very hard to use the event loop from a child program, as the |
125 | becomes very hard to use the event loop from a child program, as the |
126 | watchers already exist but are only meaningful in the parent. Worse, a |
126 | watchers already exist but are only meaningful in the parent. Worse, a |
127 | module might want to use such a module, not knowing whether another module |
127 | module might want to use such a module, not knowing whether another module |
128 | or the main program also does, leading to problems. |
128 | or the main program also does, leading to problems. |
129 | |
129 | |
|
|
130 | Apart from event loops, graphical toolkits also commonly fall into the |
|
|
131 | "unsafe module" category, or just about anything that communicates with |
|
|
132 | the external world, such as network libraries and file I/O modules, which |
|
|
133 | usually don't like being copied and then allowed to continue in two |
|
|
134 | processes. |
|
|
135 | |
130 | With this module only the main program is allowed to create new processes |
136 | With this module only the main program is allowed to create new processes |
131 | by forking (because only the main program can know when it is still safe |
137 | by forking (because only the main program can know when it is still safe |
132 | to do so) - all other processes are created via fork+exec, which makes it |
138 | to do so) - all other processes are created via fork+exec, which makes it |
133 | possible to use modules such as event loops or window interfaces safely. |
139 | possible to use modules such as event loops or window interfaces safely. |
134 | |
140 | |
… | |
… | |
146 | |
152 | |
147 | # now $master_filehandle is connected to the |
153 | # now $master_filehandle is connected to the |
148 | # $slave_filehandle in the new process. |
154 | # $slave_filehandle in the new process. |
149 | }); |
155 | }); |
150 | |
156 | |
151 | # MyModule::worker might look like this |
157 | MyModule::worker might look like this: |
|
|
158 | |
152 | sub MyModule::worker { |
159 | sub MyModule::worker { |
153 | my ($slave_filehandle) = @_; |
160 | my ($slave_filehandle) = @_; |
154 | |
161 | |
155 | # now $slave_filehandle is connected to the $master_filehandle |
162 | # now $slave_filehandle is connected to the $master_filehandle |
156 | # in the original prorcess. have fun! |
163 | # in the original prorcess. have fun! |
… | |
… | |
176 | } |
183 | } |
177 | |
184 | |
178 | # now do other things - maybe use the filehandle provided by run |
185 | # now do other things - maybe use the filehandle provided by run |
179 | # to wait for the processes to die. or whatever. |
186 | # to wait for the processes to die. or whatever. |
180 | |
187 | |
181 | # My::Server::run might look like this |
188 | My::Server::run might look like this: |
|
|
189 | |
182 | sub My::Server::run { |
190 | sub My::Server::run { |
183 | my ($slave, $listener, $id) = @_; |
191 | my ($slave, $listener, $id) = @_; |
184 | |
192 | |
185 | close $slave; # we do not use the socket, so close it to save resources |
193 | close $slave; # we do not use the socket, so close it to save resources |
186 | |
194 | |
… | |
… | |
303 | my ($fork_fh) = @_; |
311 | my ($fork_fh) = @_; |
304 | }); |
312 | }); |
305 | |
313 | |
306 | =back |
314 | =back |
307 | |
315 | |
308 | =head1 FUNCTIONS |
316 | =head1 THE C<AnyEvent::Fork> CLASS |
|
|
317 | |
|
|
318 | This module exports nothing, and only implements a single class - |
|
|
319 | C<AnyEvent::Fork>. |
|
|
320 | |
|
|
321 | There are two class constructors that both create new processes - C<new> |
|
|
322 | and C<new_exec>. The C<fork> method creates a new process by forking an |
|
|
323 | existing one and could be considered a third constructor. |
|
|
324 | |
|
|
325 | Most of the remaining methods deal with preparing the new process, by |
|
|
326 | loading code, evaluating code and sending data to the new process. They |
|
|
327 | usually return the process object, so you can chain method calls. |
|
|
328 | |
|
|
329 | If a process object is destroyed before calling its C<run> method, then |
|
|
330 | the process simply exits. After C<run> is called, all responsibility is |
|
|
331 | passed to the specified function. |
|
|
332 | |
|
|
333 | As long as there is any outstanding work to be done, process objects |
|
|
334 | resist being destroyed, so there is no reason to store them unless you |
|
|
335 | need them later - configure and forget works just fine. |
309 | |
336 | |
310 | =over 4 |
337 | =over 4 |
311 | |
338 | |
312 | =cut |
339 | =cut |
313 | |
340 | |
… | |
… | |
323 | use IO::FDPass; |
350 | use IO::FDPass; |
324 | |
351 | |
325 | our $VERSION = 0.5; |
352 | our $VERSION = 0.5; |
326 | |
353 | |
327 | our $PERL; # the path to the perl interpreter, deduces with various forms of magic |
354 | our $PERL; # the path to the perl interpreter, deduces with various forms of magic |
328 | |
|
|
329 | =item my $pool = new AnyEvent::Fork key => value... |
|
|
330 | |
|
|
331 | Create a new process pool. The following named parameters are supported: |
|
|
332 | |
355 | |
333 | =over 4 |
356 | =over 4 |
334 | |
357 | |
335 | =back |
358 | =back |
336 | |
359 | |
… | |
… | |
432 | Create a new "empty" perl interpreter process and returns its process |
455 | Create a new "empty" perl interpreter process and returns its process |
433 | object for further manipulation. |
456 | object for further manipulation. |
434 | |
457 | |
435 | The new process is forked from a template process that is kept around |
458 | The new process is forked from a template process that is kept around |
436 | for this purpose. When it doesn't exist yet, it is created by a call to |
459 | for this purpose. When it doesn't exist yet, it is created by a call to |
437 | C<new_exec> and kept around for future calls. |
460 | C<new_exec> first and then stays around for future calls. |
438 | |
|
|
439 | When the process object is destroyed, it will release the file handle |
|
|
440 | that connects it with the new process. When the new process has not yet |
|
|
441 | called C<run>, then the process will exit. Otherwise, what happens depends |
|
|
442 | entirely on the code that is executed. |
|
|
443 | |
461 | |
444 | =cut |
462 | =cut |
445 | |
463 | |
446 | sub new { |
464 | sub new { |
447 | my $class = shift; |
465 | my $class = shift; |
… | |
… | |
544 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
562 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
545 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
563 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
546 | to clean up their zombies when they die. |
564 | to clean up their zombies when they die. |
547 | |
565 | |
548 | All other processes are not direct children, and will be cleaned up by |
566 | All other processes are not direct children, and will be cleaned up by |
549 | AnyEvent::Fork. |
567 | AnyEvent::Fork itself. |
550 | |
568 | |
551 | =cut |
569 | =cut |
552 | |
570 | |
553 | sub pid { |
571 | sub pid { |
554 | $_[0][0] |
572 | $_[0][0] |