… | |
… | |
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 | |
|
|
18 | This module allows you to create new processes, without actually forking |
|
|
19 | them from your current process (avoiding the problems of forking), but |
|
|
20 | preserving most of the advantages of fork. |
|
|
21 | |
|
|
22 | It can be used to create new worker processes or new independent |
|
|
23 | subprocesses for short- and long-running jobs, process pools (e.g. for use |
|
|
24 | in pre-forked servers) but also to spawn new external processes (such as |
|
|
25 | CGI scripts from a web server), which can be faster (and more well behaved) |
|
|
26 | than using fork+exec in big processes. |
|
|
27 | |
|
|
28 | Special care has been taken to make this module useful from other modules, |
|
|
29 | while still supporting specialised environments such as L<App::Staticperl> |
|
|
30 | or L<PAR::Packer>. |
|
|
31 | |
|
|
32 | =head1 WHAT THIS MODULE IS NOT |
|
|
33 | |
|
|
34 | This module only creates processes and lets you pass file handles and |
|
|
35 | strings to it, and run perl code. It does not implement any kind of RPC - |
|
|
36 | there is no back channel from the process back to you, and there is no RPC |
|
|
37 | or message passing going on. |
|
|
38 | |
|
|
39 | If you need some form of RPC, you can either implement it yourself |
|
|
40 | in whatever way you like, use some message-passing module such |
|
|
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
|
|
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
|
|
43 | and 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 |
104 | This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to |
70 | # and stderr to the communications socket. it is usually faster |
105 | the communications socket. It is usually faster than fork+exec, but still |
71 | # than fork+exec, but still let's you prepare the environment. |
106 | let'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 | |
|
|
96 | This module allows you to create new processes, without actually forking |
|
|
97 | them from your current process (avoiding the problems of forking), but |
|
|
98 | preserving most of the advantages of fork. |
|
|
99 | |
|
|
100 | It can be used to create new worker processes or new independent |
|
|
101 | subprocesses for short- and long-running jobs, process pools (e.g. for use |
|
|
102 | in pre-forked servers) but also to spawn new external processes (such as |
|
|
103 | CGI scripts from a web server), which can be faster (and more well behaved) |
|
|
104 | than using fork+exec in big processes. |
|
|
105 | |
|
|
106 | Special care has been taken to make this module useful from other modules, |
|
|
107 | while still supporting specialised environments such as L<App::Staticperl> |
|
|
108 | or L<PAR::Packer>. |
|
|
109 | |
|
|
110 | =head1 WHAT THIS MODULE IS NOT |
|
|
111 | |
|
|
112 | This module only creates processes and lets you pass file handles and |
|
|
113 | strings to it, and run perl code. It does not implement any kind of RPC - |
|
|
114 | there is no back channel from the process back to you, and there is no RPC |
|
|
115 | or message passing going on. |
|
|
116 | |
|
|
117 | If you need some form of RPC, you can either implement it yourself |
|
|
118 | in whatever way you like, use some message-passing module such |
|
|
119 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
|
|
120 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
|
|
121 | and so on. |
|
|
122 | |
128 | |
123 | =head1 PROBLEM STATEMENT |
129 | =head1 PROBLEM STATEMENT |
124 | |
130 | |
125 | There are two ways to implement parallel processing on UNIX like operating |
131 | There are two ways to implement parallel processing on UNIX like operating |
126 | systems - fork and process, and fork+exec and process. They have different |
132 | systems - fork and process, and fork+exec and process. They have different |