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.30 by root, Sat Apr 6 09:28:45 2013 UTC vs.
Revision 1.34 by root, Sat Apr 6 09:34:53 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines