ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork/Fork.pm
(Generate patch)

Comparing AnyEvent-Fork/Fork.pm (file contents):
Revision 1.25 by root, Sat Apr 6 08:55:16 2013 UTC vs.
Revision 1.30 by root, Sat Apr 6 09:28:45 2013 UTC

125becomes very hard to use the event loop from a child program, as the 125becomes very hard to use the event loop from a child program, as the
126watchers already exist but are only meaningful in the parent. Worse, a 126watchers already exist but are only meaningful in the parent. Worse, a
127module might want to use such a module, not knowing whether another module 127module might want to use such a module, not knowing whether another module
128or the main program also does, leading to problems. 128or the main program also does, leading to problems.
129 129
130Apart from event loops, graphical toolkits also commonly fall into the
131"unsafe module" category, or just about anything that communicates with
132the external world, such as network libraries and file I/O modules, which
133usually don't like being copied and then allowed to continue in two
134processes.
135
130With this module only the main program is allowed to create new processes 136With this module only the main program is allowed to create new processes
131by forking (because only the main program can know when it is still safe 137by forking (because only the main program can know when it is still safe
132to do so) - all other processes are created via fork+exec, which makes it 138to do so) - all other processes are created via fork+exec, which makes it
133possible to use modules such as event loops or window interfaces safely. 139possible 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 157MyModule::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 188My::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
318This module exports nothing, and only implements a single class -
319C<AnyEvent::Fork>.
320
321There are two class constructors that both create new processes - C<new>
322and C<new_exec>. The C<fork> method creates a new process by forking an
323existing one and could be considered a third constructor.
324
325Most of the remaining methods deal with preparing the new process, by
326loading code, evaluating code and sending data to the new process. They
327usually return the process object, so you can chain method calls.
328
329If a process object is destroyed before calling its C<run> method, then
330the process simply exits. After C<run> is called, all responsibility is
331passed to the specified function.
332
333As long as there is any outstanding work to be done, process objects
334resist being destroyed, so there is no reason to store them unless you
335need them later - configure and forget works just fine.
309 336
310=over 4 337=over 4
311 338
312=cut 339=cut
313 340
323use IO::FDPass; 350use IO::FDPass;
324 351
325our $VERSION = 0.5; 352our $VERSION = 0.5;
326 353
327our $PERL; # the path to the perl interpreter, deduces with various forms of magic 354our $PERL; # the path to the perl interpreter, deduces with various forms of magic
328
329=item my $pool = new AnyEvent::Fork key => value...
330
331Create 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
432Create a new "empty" perl interpreter process and returns its process 455Create a new "empty" perl interpreter process and returns its process
433object for further manipulation. 456object for further manipulation.
434 457
435The new process is forked from a template process that is kept around 458The new process is forked from a template process that is kept around
436for this purpose. When it doesn't exist yet, it is created by a call to 459for this purpose. When it doesn't exist yet, it is created by a call to
437C<new_exec> and kept around for future calls. 460C<new_exec> first and then stays around for future calls.
438
439When the process object is destroyed, it will release the file handle
440that connects it with the new process. When the new process has not yet
441called C<run>, then the process will exit. Otherwise, what happens depends
442entirely on the code that is executed.
443 461
444=cut 462=cut
445 463
446sub new { 464sub new {
447 my $class = shift; 465 my $class = shift;
544Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and 562Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and
545L<AnyEvent::Fork::Template> are direct children, and you are responsible 563L<AnyEvent::Fork::Template> are direct children, and you are responsible
546to clean up their zombies when they die. 564to clean up their zombies when they die.
547 565
548All other processes are not direct children, and will be cleaned up by 566All other processes are not direct children, and will be cleaned up by
549AnyEvent::Fork. 567AnyEvent::Fork itself.
550 568
551=cut 569=cut
552 570
553sub pid { 571sub pid {
554 $_[0][0] 572 $_[0][0]

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines