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.23 by root, Sat Apr 6 08:29:43 2013 UTC vs.
Revision 1.24 by root, Sat Apr 6 08:32:23 2013 UTC

4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::Fork; 7 use AnyEvent::Fork;
8 8
9 ################################################################## 9 AnyEvent::Fork
10 ->new
11 ->require ("MyModule")
12 ->run ("MyModule::server", my $cv = AE::cv);
13
14 my $fh = $cv->recv;
15
16=head1 DESCRIPTION
17
18This module allows you to create new processes, without actually forking
19them from your current process (avoiding the problems of forking), but
20preserving most of the advantages of fork.
21
22It can be used to create new worker processes or new independent
23subprocesses for short- and long-running jobs, process pools (e.g. for use
24in pre-forked servers) but also to spawn new external processes (such as
25CGI scripts from a web server), which can be faster (and more well behaved)
26than using fork+exec in big processes.
27
28Special care has been taken to make this module useful from other modules,
29while still supporting specialised environments such as L<App::Staticperl>
30or L<PAR::Packer>.
31
32=head1 WHAT THIS MODULE IS NOT
33
34This module only creates processes and lets you pass file handles and
35strings to it, and run perl code. It does not implement any kind of RPC -
36there is no back channel from the process back to you, and there is no RPC
37or message passing going on.
38
39If you need some form of RPC, you can either implement it yourself
40in whatever way you like, use some message-passing module such
41as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use
42L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages,
43and so on.
44
45=head1 EXAMPLES
46
10 # create a single new process, tell it to run your worker function 47=head2 Create a single new process, tell it to run your worker function.
11 48
12 AnyEvent::Fork 49 AnyEvent::Fork
13 ->new 50 ->new
14 ->require ("MyModule") 51 ->require ("MyModule")
15 ->run ("MyModule::worker, sub { 52 ->run ("MyModule::worker, sub {
25 62
26 # now $slave_filehandle is connected to the $master_filehandle 63 # now $slave_filehandle is connected to the $master_filehandle
27 # in the original prorcess. have fun! 64 # in the original prorcess. have fun!
28 } 65 }
29 66
30 ##################################################################
31 # create a pool of server processes all accepting on the same socket 67=head2 Create a pool of server processes all accepting on the same socket.
32 68
33 # create listener socket 69 # create listener socket
34 my $listener = ...; 70 my $listener = ...;
35 71
36 # create a pool template, initialise it and give it the socket 72 # create a pool template, initialise it and give it the socket
61 while (my $socket = $listener->accept) { 97 while (my $socket = $listener->accept) {
62 # do sth. with new socket 98 # do sth. with new socket
63 } 99 }
64 } 100 }
65 101
66 ##################################################################
67 # use AnyEvent::Fork as a faster fork+exec 102=head2 use AnyEvent::Fork as a faster fork+exec
68 103
69 # this runs /bin/echo hi, with stdout redirected to /tmp/log 104This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to
70 # and stderr to the communications socket. it is usually faster 105the communications socket. It is usually faster than fork+exec, but still
71 # than fork+exec, but still let's you prepare the environment. 106let's you prepare the environment.
72 107
73 open my $output, ">/tmp/log" or die "$!"; 108 open my $output, ">/tmp/log" or die "$!";
74 109
75 AnyEvent::Fork 110 AnyEvent::Fork
76 ->new 111 ->new
88 ->send_fh ($output) 123 ->send_fh ($output)
89 ->send_arg ("/bin/echo", "hi") 124 ->send_arg ("/bin/echo", "hi")
90 ->run ("run", my $cv = AE::cv); 125 ->run ("run", my $cv = AE::cv);
91 126
92 my $stderr = $cv->recv; 127 my $stderr = $cv->recv;
93
94=head1 DESCRIPTION
95
96This module allows you to create new processes, without actually forking
97them from your current process (avoiding the problems of forking), but
98preserving most of the advantages of fork.
99
100It can be used to create new worker processes or new independent
101subprocesses for short- and long-running jobs, process pools (e.g. for use
102in pre-forked servers) but also to spawn new external processes (such as
103CGI scripts from a web server), which can be faster (and more well behaved)
104than using fork+exec in big processes.
105
106Special care has been taken to make this module useful from other modules,
107while still supporting specialised environments such as L<App::Staticperl>
108or L<PAR::Packer>.
109
110=head1 WHAT THIS MODULE IS NOT
111
112This module only creates processes and lets you pass file handles and
113strings to it, and run perl code. It does not implement any kind of RPC -
114there is no back channel from the process back to you, and there is no RPC
115or message passing going on.
116
117If you need some form of RPC, you can either implement it yourself
118in whatever way you like, use some message-passing module such
119as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use
120L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages,
121and so on.
122 128
123=head1 PROBLEM STATEMENT 129=head1 PROBLEM STATEMENT
124 130
125There are two ways to implement parallel processing on UNIX like operating 131There are two ways to implement parallel processing on UNIX like operating
126systems - fork and process, and fork+exec and process. They have different 132systems - fork and process, and fork+exec and process. They have different

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines