1 |
root |
1.2 |
NAME |
2 |
|
|
AnyEvent::Fork - everything you wanted to use fork() for, but couldn't |
3 |
|
|
|
4 |
|
|
SYNOPSIS |
5 |
|
|
use AnyEvent::Fork; |
6 |
|
|
|
7 |
root |
1.5 |
AnyEvent::Fork |
8 |
|
|
->new |
9 |
|
|
->require ("MyModule") |
10 |
|
|
->run ("MyModule::server", my $cv = AE::cv); |
11 |
|
|
|
12 |
|
|
my $fh = $cv->recv; |
13 |
|
|
|
14 |
|
|
DESCRIPTION |
15 |
|
|
This module allows you to create new processes, without actually forking |
16 |
|
|
them from your current process (avoiding the problems of forking), but |
17 |
|
|
preserving most of the advantages of fork. |
18 |
|
|
|
19 |
|
|
It can be used to create new worker processes or new independent |
20 |
|
|
subprocesses for short- and long-running jobs, process pools (e.g. for |
21 |
|
|
use in pre-forked servers) but also to spawn new external processes |
22 |
|
|
(such as CGI scripts from a web server), which can be faster (and more |
23 |
|
|
well behaved) than using fork+exec in big processes. |
24 |
|
|
|
25 |
|
|
Special care has been taken to make this module useful from other |
26 |
|
|
modules, while still supporting specialised environments such as |
27 |
|
|
App::Staticperl or PAR::Packer. |
28 |
|
|
|
29 |
|
|
WHAT THIS MODULE IS NOT |
30 |
|
|
This module only creates processes and lets you pass file handles and |
31 |
|
|
strings to it, and run perl code. It does not implement any kind of RPC |
32 |
|
|
- there is no back channel from the process back to you, and there is no |
33 |
|
|
RPC or message passing going on. |
34 |
|
|
|
35 |
root |
1.6 |
If you need some form of RPC, you could use the AnyEvent::Fork::RPC |
36 |
|
|
companion module, which adds simple RPC/job queueing to a process |
37 |
|
|
created by this module. |
38 |
|
|
|
39 |
|
|
Or you can implement it yourself in whatever way you like, use some |
40 |
|
|
message-passing module such as AnyEvent::MP, some pipe such as |
41 |
|
|
AnyEvent::ZeroMQ, use AnyEvent::Handle on both sides to send e.g. JSON |
42 |
|
|
or Storable messages, and so on. |
43 |
root |
1.5 |
|
44 |
|
|
COMPARISON TO OTHER MODULES |
45 |
|
|
There is an abundance of modules on CPAN that do "something fork", such |
46 |
|
|
as Parallel::ForkManager, AnyEvent::ForkManager, AnyEvent::Worker or |
47 |
|
|
AnyEvent::Subprocess. There are modules that implement their own process |
48 |
|
|
management, such as AnyEvent::DBI. |
49 |
|
|
|
50 |
|
|
The problems that all these modules try to solve are real, however, none |
51 |
|
|
of them (from what I have seen) tackle the very real problems of |
52 |
|
|
unwanted memory sharing, efficiency, not being able to use event |
53 |
|
|
processing or similar modules in the processes they create. |
54 |
|
|
|
55 |
|
|
This module doesn't try to replace any of them - instead it tries to |
56 |
|
|
solve the problem of creating processes with a minimum of fuss and |
57 |
|
|
overhead (and also luxury). Ideally, most of these would use |
58 |
|
|
AnyEvent::Fork internally, except they were written before AnyEvent:Fork |
59 |
|
|
was available, so obviously had to roll their own. |
60 |
|
|
|
61 |
|
|
PROBLEM STATEMENT |
62 |
|
|
There are two traditional ways to implement parallel processing on UNIX |
63 |
|
|
like operating systems - fork and process, and fork+exec and process. |
64 |
|
|
They have different advantages and disadvantages that I describe below, |
65 |
|
|
together with how this module tries to mitigate the disadvantages. |
66 |
|
|
|
67 |
|
|
Forking from a big process can be very slow. |
68 |
|
|
A 5GB process needs 0.05s to fork on my 3.6GHz amd64 GNU/Linux box. |
69 |
|
|
This overhead is often shared with exec (because you have to fork |
70 |
|
|
first), but in some circumstances (e.g. when vfork is used), |
71 |
|
|
fork+exec can be much faster. |
72 |
|
|
|
73 |
|
|
This module can help here by telling a small(er) helper process to |
74 |
|
|
fork, which is faster then forking the main process, and also uses |
75 |
|
|
vfork where possible. This gives the speed of vfork, with the |
76 |
|
|
flexibility of fork. |
77 |
root |
1.2 |
|
78 |
root |
1.5 |
Forking usually creates a copy-on-write copy of the parent process. |
79 |
|
|
For example, modules or data files that are loaded will not use |
80 |
|
|
additional memory after a fork. When exec'ing a new process, modules |
81 |
|
|
and data files might need to be loaded again, at extra CPU and |
82 |
|
|
memory cost. But when forking, literally all data structures are |
83 |
|
|
copied - if the program frees them and replaces them by new data, |
84 |
|
|
the child processes will retain the old version even if it isn't |
85 |
|
|
used, which can suddenly and unexpectedly increase memory usage when |
86 |
|
|
freeing memory. |
87 |
|
|
|
88 |
|
|
The trade-off is between more sharing with fork (which can be good |
89 |
|
|
or bad), and no sharing with exec. |
90 |
|
|
|
91 |
|
|
This module allows the main program to do a controlled fork, and |
92 |
|
|
allows modules to exec processes safely at any time. When creating a |
93 |
|
|
custom process pool you can take advantage of data sharing via fork |
94 |
|
|
without risking to share large dynamic data structures that will |
95 |
|
|
blow up child memory usage. |
96 |
|
|
|
97 |
|
|
In other words, this module puts you into control over what is being |
98 |
|
|
shared and what isn't, at all times. |
99 |
|
|
|
100 |
|
|
Exec'ing a new perl process might be difficult. |
101 |
|
|
For example, it is not easy to find the correct path to the perl |
102 |
|
|
interpreter - $^X might not be a perl interpreter at all. |
103 |
|
|
|
104 |
|
|
This module tries hard to identify the correct path to the perl |
105 |
|
|
interpreter. With a cooperative main program, exec'ing the |
106 |
|
|
interpreter might not even be necessary, but even without help from |
107 |
|
|
the main program, it will still work when used from a module. |
108 |
|
|
|
109 |
|
|
Exec'ing a new perl process might be slow, as all necessary modules have |
110 |
|
|
to be loaded from disk again, with no guarantees of success. |
111 |
|
|
Long running processes might run into problems when perl is upgraded |
112 |
|
|
and modules are no longer loadable because they refer to a different |
113 |
|
|
perl version, or parts of a distribution are newer than the ones |
114 |
|
|
already loaded. |
115 |
|
|
|
116 |
|
|
This module supports creating pre-initialised perl processes to be |
117 |
|
|
used as a template for new processes. |
118 |
|
|
|
119 |
|
|
Forking might be impossible when a program is running. |
120 |
|
|
For example, POSIX makes it almost impossible to fork from a |
121 |
|
|
multi-threaded program while doing anything useful in the child - in |
122 |
|
|
fact, if your perl program uses POSIX threads (even indirectly via |
123 |
|
|
e.g. IO::AIO or threads), you cannot call fork on the perl level |
124 |
|
|
anymore without risking corruption issues on a number of operating |
125 |
|
|
systems. |
126 |
|
|
|
127 |
|
|
This module can safely fork helper processes at any time, by calling |
128 |
|
|
fork+exec in C, in a POSIX-compatible way (via Proc::FastSpawn). |
129 |
|
|
|
130 |
|
|
Parallel processing with fork might be inconvenient or difficult to |
131 |
|
|
implement. Modules might not work in both parent and child. |
132 |
|
|
For example, when a program uses an event loop and creates watchers |
133 |
|
|
it becomes very hard to use the event loop from a child program, as |
134 |
|
|
the watchers already exist but are only meaningful in the parent. |
135 |
|
|
Worse, a module might want to use such a module, not knowing whether |
136 |
|
|
another module or the main program also does, leading to problems. |
137 |
|
|
|
138 |
|
|
Apart from event loops, graphical toolkits also commonly fall into |
139 |
|
|
the "unsafe module" category, or just about anything that |
140 |
|
|
communicates with the external world, such as network libraries and |
141 |
|
|
file I/O modules, which usually don't like being copied and then |
142 |
|
|
allowed to continue in two processes. |
143 |
|
|
|
144 |
|
|
With this module only the main program is allowed to create new |
145 |
|
|
processes by forking (because only the main program can know when it |
146 |
|
|
is still safe to do so) - all other processes are created via |
147 |
|
|
fork+exec, which makes it possible to use modules such as event |
148 |
|
|
loops or window interfaces safely. |
149 |
|
|
|
150 |
|
|
EXAMPLES |
151 |
|
|
Create a single new process, tell it to run your worker function. |
152 |
root |
1.2 |
AnyEvent::Fork |
153 |
|
|
->new |
154 |
|
|
->require ("MyModule") |
155 |
|
|
->run ("MyModule::worker, sub { |
156 |
|
|
my ($master_filehandle) = @_; |
157 |
|
|
|
158 |
|
|
# now $master_filehandle is connected to the |
159 |
|
|
# $slave_filehandle in the new process. |
160 |
|
|
}); |
161 |
|
|
|
162 |
root |
1.5 |
"MyModule" might look like this: |
163 |
|
|
|
164 |
|
|
package MyModule; |
165 |
|
|
|
166 |
|
|
sub worker { |
167 |
root |
1.2 |
my ($slave_filehandle) = @_; |
168 |
|
|
|
169 |
|
|
# now $slave_filehandle is connected to the $master_filehandle |
170 |
|
|
# in the original prorcess. have fun! |
171 |
|
|
} |
172 |
|
|
|
173 |
root |
1.5 |
Create a pool of server processes all accepting on the same socket. |
174 |
root |
1.2 |
# create listener socket |
175 |
|
|
my $listener = ...; |
176 |
|
|
|
177 |
|
|
# create a pool template, initialise it and give it the socket |
178 |
|
|
my $pool = AnyEvent::Fork |
179 |
|
|
->new |
180 |
|
|
->require ("Some::Stuff", "My::Server") |
181 |
|
|
->send_fh ($listener); |
182 |
|
|
|
183 |
|
|
# now create 10 identical workers |
184 |
|
|
for my $id (1..10) { |
185 |
|
|
$pool |
186 |
|
|
->fork |
187 |
|
|
->send_arg ($id) |
188 |
|
|
->run ("My::Server::run"); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
# now do other things - maybe use the filehandle provided by run |
192 |
|
|
# to wait for the processes to die. or whatever. |
193 |
|
|
|
194 |
root |
1.5 |
"My::Server" might look like this: |
195 |
|
|
|
196 |
|
|
package My::Server; |
197 |
|
|
|
198 |
|
|
sub run { |
199 |
root |
1.2 |
my ($slave, $listener, $id) = @_; |
200 |
|
|
|
201 |
|
|
close $slave; # we do not use the socket, so close it to save resources |
202 |
|
|
|
203 |
|
|
# we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
204 |
|
|
# or anything we usually couldn't do in a process forked normally. |
205 |
|
|
while (my $socket = $listener->accept) { |
206 |
|
|
# do sth. with new socket |
207 |
|
|
} |
208 |
|
|
} |
209 |
|
|
|
210 |
root |
1.5 |
use AnyEvent::Fork as a faster fork+exec |
211 |
root |
1.6 |
This runs "/bin/echo hi", with standard output redirected to /tmp/log |
212 |
root |
1.5 |
and standard error redirected to the communications socket. It is |
213 |
|
|
usually faster than fork+exec, but still lets you prepare the |
214 |
|
|
environment. |
215 |
root |
1.2 |
|
216 |
root |
1.5 |
open my $output, ">/tmp/log" or die "$!"; |
217 |
root |
1.2 |
|
218 |
root |
1.5 |
AnyEvent::Fork |
219 |
|
|
->new |
220 |
|
|
->eval (' |
221 |
|
|
# compile a helper function for later use |
222 |
|
|
sub run { |
223 |
|
|
my ($fh, $output, @cmd) = @_; |
224 |
|
|
|
225 |
|
|
# perl will clear close-on-exec on STDOUT/STDERR |
226 |
|
|
open STDOUT, ">&", $output or die; |
227 |
|
|
open STDERR, ">&", $fh or die; |
228 |
|
|
|
229 |
|
|
exec @cmd; |
230 |
|
|
} |
231 |
|
|
') |
232 |
|
|
->send_fh ($output) |
233 |
|
|
->send_arg ("/bin/echo", "hi") |
234 |
|
|
->run ("run", my $cv = AE::cv); |
235 |
root |
1.4 |
|
236 |
root |
1.5 |
my $stderr = $cv->recv; |
237 |
root |
1.2 |
|
238 |
|
|
CONCEPTS |
239 |
|
|
This module can create new processes either by executing a new perl |
240 |
|
|
process, or by forking from an existing "template" process. |
241 |
|
|
|
242 |
root |
1.6 |
All these processes are called "child processes" (whether they are |
243 |
|
|
direct children or not), while the process that manages them is called |
244 |
|
|
the "parent process". |
245 |
|
|
|
246 |
root |
1.2 |
Each such process comes with its own file handle that can be used to |
247 |
|
|
communicate with it (it's actually a socket - one end in the new |
248 |
|
|
process, one end in the main process), and among the things you can do |
249 |
|
|
in it are load modules, fork new processes, send file handles to it, and |
250 |
|
|
execute functions. |
251 |
|
|
|
252 |
|
|
There are multiple ways to create additional processes to execute some |
253 |
|
|
jobs: |
254 |
|
|
|
255 |
|
|
fork a new process from the "default" template process, load code, run |
256 |
|
|
it |
257 |
|
|
This module has a "default" template process which it executes when |
258 |
|
|
it is needed the first time. Forking from this process shares the |
259 |
|
|
memory used for the perl interpreter with the new process, but |
260 |
|
|
loading modules takes time, and the memory is not shared with |
261 |
|
|
anything else. |
262 |
|
|
|
263 |
|
|
This is ideal for when you only need one extra process of a kind, |
264 |
root |
1.4 |
with the option of starting and stopping it on demand. |
265 |
root |
1.2 |
|
266 |
|
|
Example: |
267 |
|
|
|
268 |
|
|
AnyEvent::Fork |
269 |
|
|
->new |
270 |
|
|
->require ("Some::Module") |
271 |
|
|
->run ("Some::Module::run", sub { |
272 |
|
|
my ($fork_fh) = @_; |
273 |
|
|
}); |
274 |
|
|
|
275 |
|
|
fork a new template process, load code, then fork processes off of it |
276 |
|
|
and run the code |
277 |
|
|
When you need to have a bunch of processes that all execute the same |
278 |
|
|
(or very similar) tasks, then a good way is to create a new template |
279 |
|
|
process for them, loading all the modules you need, and then create |
280 |
|
|
your worker processes from this new template process. |
281 |
|
|
|
282 |
|
|
This way, all code (and data structures) that can be shared (e.g. |
283 |
|
|
the modules you loaded) is shared between the processes, and each |
284 |
|
|
new process consumes relatively little memory of its own. |
285 |
|
|
|
286 |
|
|
The disadvantage of this approach is that you need to create a |
287 |
|
|
template process for the sole purpose of forking new processes from |
288 |
root |
1.4 |
it, but if you only need a fixed number of processes you can create |
289 |
root |
1.2 |
them, and then destroy the template process. |
290 |
|
|
|
291 |
|
|
Example: |
292 |
|
|
|
293 |
|
|
my $template = AnyEvent::Fork->new->require ("Some::Module"); |
294 |
|
|
|
295 |
|
|
for (1..10) { |
296 |
|
|
$template->fork->run ("Some::Module::run", sub { |
297 |
|
|
my ($fork_fh) = @_; |
298 |
|
|
}); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
# at this point, you can keep $template around to fork new processes |
302 |
|
|
# later, or you can destroy it, which causes it to vanish. |
303 |
|
|
|
304 |
|
|
execute a new perl interpreter, load some code, run it |
305 |
|
|
This is relatively slow, and doesn't allow you to share memory |
306 |
|
|
between multiple processes. |
307 |
|
|
|
308 |
|
|
The only advantage is that you don't have to have a template process |
309 |
|
|
hanging around all the time to fork off some new processes, which |
310 |
|
|
might be an advantage when there are long time spans where no extra |
311 |
|
|
processes are needed. |
312 |
|
|
|
313 |
|
|
Example: |
314 |
|
|
|
315 |
|
|
AnyEvent::Fork |
316 |
|
|
->new_exec |
317 |
|
|
->require ("Some::Module") |
318 |
|
|
->run ("Some::Module::run", sub { |
319 |
|
|
my ($fork_fh) = @_; |
320 |
|
|
}); |
321 |
|
|
|
322 |
root |
1.5 |
THE "AnyEvent::Fork" CLASS |
323 |
|
|
This module exports nothing, and only implements a single class - |
324 |
|
|
"AnyEvent::Fork". |
325 |
|
|
|
326 |
|
|
There are two class constructors that both create new processes - "new" |
327 |
|
|
and "new_exec". The "fork" method creates a new process by forking an |
328 |
|
|
existing one and could be considered a third constructor. |
329 |
|
|
|
330 |
|
|
Most of the remaining methods deal with preparing the new process, by |
331 |
|
|
loading code, evaluating code and sending data to the new process. They |
332 |
|
|
usually return the process object, so you can chain method calls. |
333 |
|
|
|
334 |
|
|
If a process object is destroyed before calling its "run" method, then |
335 |
|
|
the process simply exits. After "run" is called, all responsibility is |
336 |
|
|
passed to the specified function. |
337 |
|
|
|
338 |
|
|
As long as there is any outstanding work to be done, process objects |
339 |
|
|
resist being destroyed, so there is no reason to store them unless you |
340 |
|
|
need them later - configure and forget works just fine. |
341 |
|
|
|
342 |
root |
1.6 |
my $proc = new AnyEvent::Fork |
343 |
root |
1.2 |
Create a new "empty" perl interpreter process and returns its |
344 |
|
|
process object for further manipulation. |
345 |
|
|
|
346 |
|
|
The new process is forked from a template process that is kept |
347 |
|
|
around for this purpose. When it doesn't exist yet, it is created by |
348 |
root |
1.5 |
a call to "new_exec" first and then stays around for future calls. |
349 |
root |
1.2 |
|
350 |
root |
1.6 |
$new_proc = $proc->fork |
351 |
root |
1.2 |
Forks $proc, creating a new process, and returns the process object |
352 |
|
|
of the new process. |
353 |
|
|
|
354 |
|
|
If any of the "send_" functions have been called before fork, then |
355 |
|
|
they will be cloned in the child. For example, in a pre-forked |
356 |
|
|
server, you might "send_fh" the listening socket into the template |
357 |
|
|
process, and then keep calling "fork" and "run". |
358 |
|
|
|
359 |
root |
1.6 |
my $proc = new_exec AnyEvent::Fork |
360 |
root |
1.2 |
Create a new "empty" perl interpreter process and returns its |
361 |
|
|
process object for further manipulation. |
362 |
|
|
|
363 |
|
|
Unlike the "new" method, this method *always* spawns a new perl |
364 |
|
|
process (except in some cases, see AnyEvent::Fork::Early for |
365 |
|
|
details). This reduces the amount of memory sharing that is |
366 |
|
|
possible, and is also slower. |
367 |
|
|
|
368 |
|
|
You should use "new" whenever possible, except when having a |
369 |
|
|
template process around is unacceptable. |
370 |
|
|
|
371 |
root |
1.4 |
The path to the perl interpreter is divined using various methods - |
372 |
root |
1.2 |
first $^X is investigated to see if the path ends with something |
373 |
|
|
that sounds as if it were the perl interpreter. Failing this, the |
374 |
|
|
module falls back to using $Config::Config{perlpath}. |
375 |
|
|
|
376 |
root |
1.6 |
$pid = $proc->pid |
377 |
root |
1.4 |
Returns the process id of the process *iff it is a direct child of |
378 |
root |
1.5 |
the process running AnyEvent::Fork*, and "undef" otherwise. |
379 |
root |
1.4 |
|
380 |
|
|
Normally, only processes created via "AnyEvent::Fork->new_exec" and |
381 |
|
|
AnyEvent::Fork::Template are direct children, and you are |
382 |
|
|
responsible to clean up their zombies when they die. |
383 |
|
|
|
384 |
|
|
All other processes are not direct children, and will be cleaned up |
385 |
root |
1.5 |
by AnyEvent::Fork itself. |
386 |
|
|
|
387 |
root |
1.6 |
$proc = $proc->eval ($perlcode, @args) |
388 |
|
|
Evaluates the given $perlcode as ... Perl code, while setting @_ to |
389 |
root |
1.5 |
the strings specified by @args, in the "main" package. |
390 |
root |
1.2 |
|
391 |
|
|
This call is meant to do any custom initialisation that might be |
392 |
|
|
required (for example, the "require" method uses it). It's not |
393 |
|
|
supposed to be used to completely take over the process, use "run" |
394 |
|
|
for that. |
395 |
|
|
|
396 |
|
|
The code will usually be executed after this call returns, and there |
397 |
|
|
is no way to pass anything back to the calling process. Any |
398 |
|
|
evaluation errors will be reported to stderr and cause the process |
399 |
|
|
to exit. |
400 |
|
|
|
401 |
root |
1.5 |
If you want to execute some code (that isn't in a module) to take |
402 |
|
|
over the process, you should compile a function via "eval" first, |
403 |
|
|
and then call it via "run". This also gives you access to any |
404 |
|
|
arguments passed via the "send_xxx" methods, such as file handles. |
405 |
|
|
See the "use AnyEvent::Fork as a faster fork+exec" example to see it |
406 |
|
|
in action. |
407 |
|
|
|
408 |
root |
1.2 |
Returns the process object for easy chaining of method calls. |
409 |
|
|
|
410 |
root |
1.6 |
$proc = $proc->require ($module, ...) |
411 |
root |
1.2 |
Tries to load the given module(s) into the process |
412 |
|
|
|
413 |
|
|
Returns the process object for easy chaining of method calls. |
414 |
|
|
|
415 |
root |
1.6 |
$proc = $proc->send_fh ($handle, ...) |
416 |
root |
1.2 |
Send one or more file handles (*not* file descriptors) to the |
417 |
|
|
process, to prepare a call to "run". |
418 |
|
|
|
419 |
root |
1.5 |
The process object keeps a reference to the handles until they have |
420 |
|
|
been passed over to the process, so you must not explicitly close |
421 |
|
|
the handles. This is most easily accomplished by simply not storing |
422 |
|
|
the file handles anywhere after passing them to this method - when |
423 |
|
|
AnyEvent::Fork is finished using them, perl will automatically close |
424 |
|
|
them. |
425 |
root |
1.2 |
|
426 |
|
|
Returns the process object for easy chaining of method calls. |
427 |
|
|
|
428 |
root |
1.4 |
Example: pass a file handle to a process, and release it without |
429 |
|
|
closing. It will be closed automatically when it is no longer used. |
430 |
root |
1.2 |
|
431 |
|
|
$proc->send_fh ($my_fh); |
432 |
|
|
undef $my_fh; # free the reference if you want, but DO NOT CLOSE IT |
433 |
|
|
|
434 |
root |
1.6 |
$proc = $proc->send_arg ($string, ...) |
435 |
root |
1.2 |
Send one or more argument strings to the process, to prepare a call |
436 |
root |
1.5 |
to "run". The strings can be any octet strings. |
437 |
root |
1.2 |
|
438 |
root |
1.4 |
The protocol is optimised to pass a moderate number of relatively |
439 |
|
|
short strings - while you can pass up to 4GB of data in one go, this |
440 |
|
|
is more meant to pass some ID information or other startup info, not |
441 |
|
|
big chunks of data. |
442 |
|
|
|
443 |
|
|
Returns the process object for easy chaining of method calls. |
444 |
root |
1.2 |
|
445 |
root |
1.6 |
$proc->run ($func, $cb->($fh)) |
446 |
root |
1.5 |
Enter the function specified by the function name in $func in the |
447 |
|
|
process. The function is called with the communication socket as |
448 |
root |
1.2 |
first argument, followed by all file handles and string arguments |
449 |
|
|
sent earlier via "send_fh" and "send_arg" methods, in the order they |
450 |
|
|
were called. |
451 |
|
|
|
452 |
root |
1.5 |
The process object becomes unusable on return from this function - |
453 |
|
|
any further method calls result in undefined behaviour. |
454 |
root |
1.2 |
|
455 |
root |
1.5 |
The function name should be fully qualified, but if it isn't, it |
456 |
|
|
will be looked up in the "main" package. |
457 |
root |
1.2 |
|
458 |
root |
1.5 |
If the called function returns, doesn't exist, or any error occurs, |
459 |
|
|
the process exits. |
460 |
|
|
|
461 |
|
|
Preparing the process is done in the background - when all commands |
462 |
|
|
have been sent, the callback is invoked with the local |
463 |
|
|
communications socket as argument. At this point you can start using |
464 |
|
|
the socket in any way you like. |
465 |
root |
1.2 |
|
466 |
|
|
If the communication socket isn't used, it should be closed on both |
467 |
|
|
sides, to save on kernel memory. |
468 |
|
|
|
469 |
|
|
The socket is non-blocking in the parent, and blocking in the newly |
470 |
root |
1.5 |
created process. The close-on-exec flag is set in both. |
471 |
|
|
|
472 |
|
|
Even if not used otherwise, the socket can be a good indicator for |
473 |
|
|
the existence of the process - if the other process exits, you get a |
474 |
|
|
readable event on it, because exiting the process closes the socket |
475 |
|
|
(if it didn't create any children using fork). |
476 |
root |
1.2 |
|
477 |
|
|
Example: create a template for a process pool, pass a few strings, |
478 |
|
|
some file handles, then fork, pass one more string, and run some |
479 |
|
|
code. |
480 |
|
|
|
481 |
|
|
my $pool = AnyEvent::Fork |
482 |
|
|
->new |
483 |
|
|
->send_arg ("str1", "str2") |
484 |
|
|
->send_fh ($fh1, $fh2); |
485 |
|
|
|
486 |
|
|
for (1..2) { |
487 |
|
|
$pool |
488 |
|
|
->fork |
489 |
|
|
->send_arg ("str3") |
490 |
|
|
->run ("Some::function", sub { |
491 |
|
|
my ($fh) = @_; |
492 |
|
|
|
493 |
|
|
# fh is nonblocking, but we trust that the OS can accept these |
494 |
root |
1.5 |
# few octets anyway. |
495 |
root |
1.2 |
syswrite $fh, "hi #$_\n"; |
496 |
|
|
|
497 |
|
|
# $fh is being closed here, as we don't store it anywhere |
498 |
|
|
}); |
499 |
|
|
} |
500 |
|
|
|
501 |
|
|
# Some::function might look like this - all parameters passed before fork |
502 |
|
|
# and after will be passed, in order, after the communications socket. |
503 |
|
|
sub Some::function { |
504 |
|
|
my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
505 |
|
|
|
506 |
root |
1.5 |
print scalar <$fh>; # prints "hi #1\n" and "hi #2\n" in any order |
507 |
root |
1.2 |
} |
508 |
|
|
|
509 |
root |
1.4 |
PERFORMANCE |
510 |
|
|
Now for some unscientific benchmark numbers (all done on an amd64 |
511 |
|
|
GNU/Linux box). These are intended to give you an idea of the relative |
512 |
|
|
performance you can expect, they are not meant to be absolute |
513 |
|
|
performance numbers. |
514 |
|
|
|
515 |
|
|
OK, so, I ran a simple benchmark that creates a socket pair, forks, |
516 |
|
|
calls exit in the child and waits for the socket to close in the parent. |
517 |
|
|
I did load AnyEvent, EV and AnyEvent::Fork, for a total process size of |
518 |
|
|
5100kB. |
519 |
|
|
|
520 |
|
|
2079 new processes per second, using manual socketpair + fork |
521 |
|
|
|
522 |
|
|
Then I did the same thing, but instead of calling fork, I called |
523 |
|
|
AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
524 |
|
|
socket form the child to close on exit. This does the same thing as |
525 |
|
|
manual socket pair + fork, except that what is forked is the template |
526 |
|
|
process (2440kB), and the socket needs to be passed to the server at the |
527 |
|
|
other end of the socket first. |
528 |
|
|
|
529 |
|
|
2307 new processes per second, using AnyEvent::Fork->new |
530 |
|
|
|
531 |
|
|
And finally, using "new_exec" instead "new", using vforks+execs to exec |
532 |
|
|
a new perl interpreter and compile the small server each time, I get: |
533 |
|
|
|
534 |
|
|
479 vfork+execs per second, using AnyEvent::Fork->new_exec |
535 |
|
|
|
536 |
|
|
So how can "AnyEvent->new" be faster than a standard fork, even though |
537 |
|
|
it uses the same operations, but adds a lot of overhead? |
538 |
|
|
|
539 |
root |
1.5 |
The difference is simply the process size: forking the 5MB process takes |
540 |
|
|
so much longer than forking the 2.5MB template process that the extra |
541 |
root |
1.6 |
overhead is canceled out. |
542 |
root |
1.4 |
|
543 |
|
|
If the benchmark process grows, the normal fork becomes even slower: |
544 |
|
|
|
545 |
root |
1.5 |
1340 new processes, manual fork of a 20MB process |
546 |
|
|
731 new processes, manual fork of a 200MB process |
547 |
|
|
235 new processes, manual fork of a 2000MB process |
548 |
root |
1.4 |
|
549 |
|
|
What that means (to me) is that I can use this module without having a |
550 |
root |
1.5 |
bad conscience because of the extra overhead required to start new |
551 |
root |
1.4 |
processes. |
552 |
|
|
|
553 |
root |
1.3 |
TYPICAL PROBLEMS |
554 |
|
|
This section lists typical problems that remain. I hope by recognising |
555 |
|
|
them, most can be avoided. |
556 |
|
|
|
557 |
root |
1.5 |
leaked file descriptors for exec'ed processes |
558 |
root |
1.3 |
POSIX systems inherit file descriptors by default when exec'ing a |
559 |
|
|
new process. While perl itself laudably sets the close-on-exec flags |
560 |
|
|
on new file handles, most C libraries don't care, and even if all |
561 |
|
|
cared, it's often not possible to set the flag in a race-free |
562 |
|
|
manner. |
563 |
|
|
|
564 |
|
|
That means some file descriptors can leak through. And since it |
565 |
|
|
isn't possible to know which file descriptors are "good" and |
566 |
root |
1.4 |
"necessary" (or even to know which file descriptors are open), there |
567 |
|
|
is no good way to close the ones that might harm. |
568 |
root |
1.3 |
|
569 |
|
|
As an example of what "harm" can be done consider a web server that |
570 |
|
|
accepts connections and afterwards some module uses AnyEvent::Fork |
571 |
|
|
for the first time, causing it to fork and exec a new process, which |
572 |
|
|
might inherit the network socket. When the server closes the socket, |
573 |
|
|
it is still open in the child (which doesn't even know that) and the |
574 |
|
|
client might conclude that the connection is still fine. |
575 |
|
|
|
576 |
|
|
For the main program, there are multiple remedies available - |
577 |
|
|
AnyEvent::Fork::Early is one, creating a process early and not using |
578 |
|
|
"new_exec" is another, as in both cases, the first process can be |
579 |
|
|
exec'ed well before many random file descriptors are open. |
580 |
|
|
|
581 |
|
|
In general, the solution for these kind of problems is to fix the |
582 |
|
|
libraries or the code that leaks those file descriptors. |
583 |
|
|
|
584 |
root |
1.4 |
Fortunately, most of these leaked descriptors do no harm, other than |
585 |
root |
1.3 |
sitting on some resources. |
586 |
|
|
|
587 |
root |
1.5 |
leaked file descriptors for fork'ed processes |
588 |
root |
1.3 |
Normally, AnyEvent::Fork does start new processes by exec'ing them, |
589 |
|
|
which closes file descriptors not marked for being inherited. |
590 |
|
|
|
591 |
|
|
However, AnyEvent::Fork::Early and AnyEvent::Fork::Template offer a |
592 |
|
|
way to create these processes by forking, and this leaks more file |
593 |
|
|
descriptors than exec'ing them, as there is no way to mark |
594 |
|
|
descriptors as "close on fork". |
595 |
|
|
|
596 |
|
|
An example would be modules like EV, IO::AIO or Gtk2. Both create |
597 |
|
|
pipes for internal uses, and Gtk2 might open a connection to the X |
598 |
|
|
server. EV and IO::AIO can deal with fork, but Gtk2 might have |
599 |
|
|
trouble with a fork. |
600 |
|
|
|
601 |
|
|
The solution is to either not load these modules before use'ing |
602 |
|
|
AnyEvent::Fork::Early or AnyEvent::Fork::Template, or to delay |
603 |
|
|
initialising them, for example, by calling "init Gtk2" manually. |
604 |
|
|
|
605 |
root |
1.5 |
exiting calls object destructors |
606 |
|
|
This only applies to users of AnyEvent::Fork:Early and |
607 |
root |
1.6 |
AnyEvent::Fork::Template, or when initialising code creates objects |
608 |
root |
1.5 |
that reference external resources. |
609 |
root |
1.4 |
|
610 |
|
|
When a process created by AnyEvent::Fork exits, it might do so by |
611 |
|
|
calling exit, or simply letting perl reach the end of the program. |
612 |
|
|
At which point Perl runs all destructors. |
613 |
|
|
|
614 |
|
|
Not all destructors are fork-safe - for example, an object that |
615 |
|
|
represents the connection to an X display might tell the X server to |
616 |
|
|
free resources, which is inconvenient when the "real" object in the |
617 |
|
|
parent still needs to use them. |
618 |
|
|
|
619 |
|
|
This is obviously not a problem for AnyEvent::Fork::Early, as you |
620 |
|
|
used it as the very first thing, right? |
621 |
|
|
|
622 |
|
|
It is a problem for AnyEvent::Fork::Template though - and the |
623 |
|
|
solution is to not create objects with nontrivial destructors that |
624 |
|
|
might have an effect outside of Perl. |
625 |
|
|
|
626 |
root |
1.2 |
PORTABILITY NOTES |
627 |
|
|
Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a |
628 |
|
|
nop, and ::Template is not going to work), and it cost a lot of blood |
629 |
|
|
and sweat to make it so, mostly due to the bloody broken perl that |
630 |
|
|
nobody seems to care about. The fork emulation is a bad joke - I have |
631 |
root |
1.4 |
yet to see something useful that you can do with it without running into |
632 |
root |
1.2 |
memory corruption issues or other braindamage. Hrrrr. |
633 |
|
|
|
634 |
root |
1.5 |
Cygwin perl is not supported at the moment due to some hilarious |
635 |
|
|
shortcomings of its API - see IO::FDPoll for more details. |
636 |
root |
1.2 |
|
637 |
root |
1.3 |
SEE ALSO |
638 |
root |
1.6 |
AnyEvent::Fork::Early, to avoid executing a perl interpreter at all |
639 |
|
|
(part of this distribution). |
640 |
root |
1.3 |
|
641 |
root |
1.6 |
AnyEvent::Fork::Template, to create a process by forking the main |
642 |
|
|
program at a convenient time (part of this distribution). |
643 |
|
|
|
644 |
|
|
AnyEvent::Fork::RPC, for simple RPC to child processes (on CPAN). |
645 |
|
|
|
646 |
|
|
AUTHOR AND CONTACT INFORMATION |
647 |
root |
1.2 |
Marc Lehmann <schmorp@schmorp.de> |
648 |
root |
1.6 |
http://software.schmorp.de/pkg/AnyEvent-Fork |
649 |
root |
1.5 |
|