… | |
… | |
67 | preserving most of the advantages of fork. |
67 | preserving most of the advantages of fork. |
68 | |
68 | |
69 | It can be used to create new worker processes or new independent |
69 | It can be used to create new worker processes or new independent |
70 | subprocesses for short- and long-running jobs, process pools (e.g. for |
70 | subprocesses for short- and long-running jobs, process pools (e.g. for |
71 | use in pre-forked servers) but also to spawn new external processes |
71 | use in pre-forked servers) but also to spawn new external processes |
72 | (such as CGI scripts from a webserver), which can be faster (and more |
72 | (such as CGI scripts from a web server), which can be faster (and more |
73 | well behaved) than using fork+exec in big processes. |
73 | well behaved) than using fork+exec in big processes. |
74 | |
74 | |
75 | Special care has been taken to make this module useful from other |
75 | Special care has been taken to make this module useful from other |
76 | modules, while still supporting specialised environments such as |
76 | modules, while still supporting specialised environments such as |
77 | App::Staticperl or PAR::Packer. |
77 | App::Staticperl or PAR::Packer. |
|
|
78 | |
|
|
79 | WHAT THIS MODULE IS NOT |
|
|
80 | This module only creates processes and lets you pass file handles and |
|
|
81 | strings to it, and run perl code. It does not implement any kind of RPC |
|
|
82 | - there is no back channel from the process back to you, and there is no |
|
|
83 | RPC or message passing going on. |
|
|
84 | |
|
|
85 | If you need some form of RPC, you can either implement it yourself in |
|
|
86 | whatever way you like, use some message-passing module such as |
|
|
87 | AnyEvent::MP, some pipe such as AnyEvent::ZeroMQ, use AnyEvent::Handle |
|
|
88 | on both sides to send e.g. JSON or Storable messages, and so on. |
78 | |
89 | |
79 | PROBLEM STATEMENT |
90 | PROBLEM STATEMENT |
80 | There are two ways to implement parallel processing on UNIX like |
91 | There are two ways to implement parallel processing on UNIX like |
81 | operating systems - fork and process, and fork+exec and process. They |
92 | operating systems - fork and process, and fork+exec and process. They |
82 | have different advantages and disadvantages that I describe below, |
93 | have different advantages and disadvantages that I describe below, |
… | |
… | |
90 | fork, or fork+exec instead. |
101 | fork, or fork+exec instead. |
91 | |
102 | |
92 | Forking usually creates a copy-on-write copy of the parent process. |
103 | Forking usually creates a copy-on-write copy of the parent process. |
93 | Memory (for example, modules or data files that have been will not take |
104 | Memory (for example, modules or data files that have been will not take |
94 | additional memory). When exec'ing a new process, modules and data files |
105 | additional memory). When exec'ing a new process, modules and data files |
95 | might need to be loaded again, at extra cpu and memory cost. Likewise |
106 | might need to be loaded again, at extra CPU and memory cost. Likewise |
96 | when forking, all data structures are copied as well - if the program |
107 | when forking, all data structures are copied as well - if the program |
97 | frees them and replaces them by new data, the child processes will |
108 | frees them and replaces them by new data, the child processes will |
98 | retain the memory even if it isn't used. |
109 | retain the memory even if it isn't used. |
99 | This module allows the main program to do a controlled fork, and |
110 | This module allows the main program to do a controlled fork, and |
100 | allows modules to exec processes safely at any time. When creating a |
111 | allows modules to exec processes safely at any time. When creating a |
… | |
… | |
110 | used as template, and also tries hard to identify the correct path |
121 | used as template, and also tries hard to identify the correct path |
111 | to the perl interpreter. With a cooperative main program, exec'ing |
122 | to the perl interpreter. With a cooperative main program, exec'ing |
112 | the interpreter might not even be necessary. |
123 | the interpreter might not even be necessary. |
113 | |
124 | |
114 | Forking might be impossible when a program is running. For example, |
125 | Forking might be impossible when a program is running. For example, |
115 | POSIX makes it almost impossible to fork from a multithreaded program |
126 | POSIX makes it almost impossible to fork from a multi-threaded program |
116 | and do anything useful in the child - strictly speaking, if your perl |
127 | and do anything useful in the child - strictly speaking, if your perl |
117 | program uses posix threads (even indirectly via e.g. IO::AIO or |
128 | program uses posix threads (even indirectly via e.g. IO::AIO or |
118 | threads), you cannot call fork on the perl level anymore, at all. |
129 | threads), you cannot call fork on the perl level anymore, at all. |
119 | This module can safely fork helper processes at any time, by caling |
130 | This module can safely fork helper processes at any time, by calling |
120 | fork+exec in C, in a POSIX-compatible way. |
131 | fork+exec in C, in a POSIX-compatible way. |
121 | |
132 | |
122 | Parallel processing with fork might be inconvenient or difficult to |
133 | Parallel processing with fork might be inconvenient or difficult to |
123 | implement. For example, when a program uses an event loop and creates |
134 | implement. For example, when a program uses an event loop and creates |
124 | watchers it becomes very hard to use the event loop from a child |
135 | watchers it becomes very hard to use the event loop from a child |
… | |
… | |
151 | memory used for the perl interpreter with the new process, but |
162 | memory used for the perl interpreter with the new process, but |
152 | loading modules takes time, and the memory is not shared with |
163 | loading modules takes time, and the memory is not shared with |
153 | anything else. |
164 | anything else. |
154 | |
165 | |
155 | This is ideal for when you only need one extra process of a kind, |
166 | This is ideal for when you only need one extra process of a kind, |
156 | with the option of starting and stipping it on demand. |
167 | with the option of starting and stopping it on demand. |
157 | |
168 | |
158 | Example: |
169 | Example: |
159 | |
170 | |
160 | AnyEvent::Fork |
171 | AnyEvent::Fork |
161 | ->new |
172 | ->new |
… | |
… | |
175 | the modules you loaded) is shared between the processes, and each |
186 | the modules you loaded) is shared between the processes, and each |
176 | new process consumes relatively little memory of its own. |
187 | new process consumes relatively little memory of its own. |
177 | |
188 | |
178 | The disadvantage of this approach is that you need to create a |
189 | The disadvantage of this approach is that you need to create a |
179 | template process for the sole purpose of forking new processes from |
190 | template process for the sole purpose of forking new processes from |
180 | it, but if you only need a fixed number of proceses you can create |
191 | it, but if you only need a fixed number of processes you can create |
181 | them, and then destroy the template process. |
192 | them, and then destroy the template process. |
182 | |
193 | |
183 | Example: |
194 | Example: |
184 | |
195 | |
185 | my $template = AnyEvent::Fork->new->require ("Some::Module"); |
196 | my $template = AnyEvent::Fork->new->require ("Some::Module"); |
… | |
… | |
248 | possible, and is also slower. |
259 | possible, and is also slower. |
249 | |
260 | |
250 | You should use "new" whenever possible, except when having a |
261 | You should use "new" whenever possible, except when having a |
251 | template process around is unacceptable. |
262 | template process around is unacceptable. |
252 | |
263 | |
253 | The path to the perl interpreter is divined usign various methods - |
264 | The path to the perl interpreter is divined using various methods - |
254 | first $^X is investigated to see if the path ends with something |
265 | first $^X is investigated to see if the path ends with something |
255 | that sounds as if it were the perl interpreter. Failing this, the |
266 | that sounds as if it were the perl interpreter. Failing this, the |
256 | module falls back to using $Config::Config{perlpath}. |
267 | module falls back to using $Config::Config{perlpath}. |
|
|
268 | |
|
|
269 | $pid = $proc->pid |
|
|
270 | Returns the process id of the process *iff it is a direct child of |
|
|
271 | the process* running AnyEvent::Fork, and "undef" otherwise. |
|
|
272 | |
|
|
273 | Normally, only processes created via "AnyEvent::Fork->new_exec" and |
|
|
274 | AnyEvent::Fork::Template are direct children, and you are |
|
|
275 | responsible to clean up their zombies when they die. |
|
|
276 | |
|
|
277 | All other processes are not direct children, and will be cleaned up |
|
|
278 | by AnyEvent::Fork. |
257 | |
279 | |
258 | $proc = $proc->eval ($perlcode, @args) |
280 | $proc = $proc->eval ($perlcode, @args) |
259 | Evaluates the given $perlcode as ... perl code, while setting @_ to |
281 | Evaluates the given $perlcode as ... perl code, while setting @_ to |
260 | the strings specified by @args. |
282 | the strings specified by @args. |
261 | |
283 | |
… | |
… | |
285 | easily accomplished by simply not storing the file handles anywhere |
307 | easily accomplished by simply not storing the file handles anywhere |
286 | after passing them to this method. |
308 | after passing them to this method. |
287 | |
309 | |
288 | Returns the process object for easy chaining of method calls. |
310 | Returns the process object for easy chaining of method calls. |
289 | |
311 | |
290 | Example: pass an fh to a process, and release it without closing. it |
312 | Example: pass a file handle to a process, and release it without |
291 | will be closed automatically when it is no longer used. |
313 | closing. It will be closed automatically when it is no longer used. |
292 | |
314 | |
293 | $proc->send_fh ($my_fh); |
315 | $proc->send_fh ($my_fh); |
294 | undef $my_fh; # free the reference if you want, but DO NOT CLOSE IT |
316 | undef $my_fh; # free the reference if you want, but DO NOT CLOSE IT |
295 | |
317 | |
296 | $proc = $proc->send_arg ($string, ...) |
318 | $proc = $proc->send_arg ($string, ...) |
297 | Send one or more argument strings to the process, to prepare a call |
319 | Send one or more argument strings to the process, to prepare a call |
298 | to "run". The strings can be any octet string. |
320 | to "run". The strings can be any octet string. |
299 | |
321 | |
|
|
322 | The protocol is optimised to pass a moderate number of relatively |
|
|
323 | short strings - while you can pass up to 4GB of data in one go, this |
|
|
324 | is more meant to pass some ID information or other startup info, not |
|
|
325 | big chunks of data. |
|
|
326 | |
300 | Returns the process object for easy chaining of emthod calls. |
327 | Returns the process object for easy chaining of method calls. |
301 | |
328 | |
302 | $proc->run ($func, $cb->($fh)) |
329 | $proc->run ($func, $cb->($fh)) |
303 | Enter the function specified by the fully qualified name in $func in |
330 | Enter the function specified by the fully qualified name in $func in |
304 | the process. The function is called with the communication socket as |
331 | the process. The function is called with the communication socket as |
305 | first argument, followed by all file handles and string arguments |
332 | first argument, followed by all file handles and string arguments |
… | |
… | |
317 | If the communication socket isn't used, it should be closed on both |
344 | If the communication socket isn't used, it should be closed on both |
318 | sides, to save on kernel memory. |
345 | sides, to save on kernel memory. |
319 | |
346 | |
320 | The socket is non-blocking in the parent, and blocking in the newly |
347 | The socket is non-blocking in the parent, and blocking in the newly |
321 | created process. The close-on-exec flag is set on both. Even if not |
348 | created process. The close-on-exec flag is set on both. Even if not |
322 | used otherwise, the socket can be a good indicator for the existance |
349 | used otherwise, the socket can be a good indicator for the existence |
323 | of the process - if the other process exits, you get a readable |
350 | of the process - if the other process exits, you get a readable |
324 | event on it, because exiting the process closes the socket (if it |
351 | event on it, because exiting the process closes the socket (if it |
325 | didn't create any children using fork). |
352 | didn't create any children using fork). |
326 | |
353 | |
327 | Example: create a template for a process pool, pass a few strings, |
354 | Example: create a template for a process pool, pass a few strings, |
… | |
… | |
354 | my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
381 | my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
355 | |
382 | |
356 | print scalar <$fh>; # prints "hi 1\n" and "hi 2\n" |
383 | print scalar <$fh>; # prints "hi 1\n" and "hi 2\n" |
357 | } |
384 | } |
358 | |
385 | |
|
|
386 | PERFORMANCE |
|
|
387 | Now for some unscientific benchmark numbers (all done on an amd64 |
|
|
388 | GNU/Linux box). These are intended to give you an idea of the relative |
|
|
389 | performance you can expect, they are not meant to be absolute |
|
|
390 | performance numbers. |
|
|
391 | |
|
|
392 | OK, so, I ran a simple benchmark that creates a socket pair, forks, |
|
|
393 | calls exit in the child and waits for the socket to close in the parent. |
|
|
394 | I did load AnyEvent, EV and AnyEvent::Fork, for a total process size of |
|
|
395 | 5100kB. |
|
|
396 | |
|
|
397 | 2079 new processes per second, using manual socketpair + fork |
|
|
398 | |
|
|
399 | Then I did the same thing, but instead of calling fork, I called |
|
|
400 | AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
|
|
401 | socket form the child to close on exit. This does the same thing as |
|
|
402 | manual socket pair + fork, except that what is forked is the template |
|
|
403 | process (2440kB), and the socket needs to be passed to the server at the |
|
|
404 | other end of the socket first. |
|
|
405 | |
|
|
406 | 2307 new processes per second, using AnyEvent::Fork->new |
|
|
407 | |
|
|
408 | And finally, using "new_exec" instead "new", using vforks+execs to exec |
|
|
409 | a new perl interpreter and compile the small server each time, I get: |
|
|
410 | |
|
|
411 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
|
|
412 | |
|
|
413 | So how can "AnyEvent->new" be faster than a standard fork, even though |
|
|
414 | it uses the same operations, but adds a lot of overhead? |
|
|
415 | |
|
|
416 | The difference is simply the process size: forking the 6MB process takes |
|
|
417 | so much longer than forking the 2.5MB template process that the overhead |
|
|
418 | introduced is canceled out. |
|
|
419 | |
|
|
420 | If the benchmark process grows, the normal fork becomes even slower: |
|
|
421 | |
|
|
422 | 1340 new processes, manual fork in a 20MB process |
|
|
423 | 731 new processes, manual fork in a 200MB process |
|
|
424 | 235 new processes, manual fork in a 2000MB process |
|
|
425 | |
|
|
426 | What that means (to me) is that I can use this module without having a |
|
|
427 | very bad conscience because of the extra overhead required to start new |
|
|
428 | processes. |
|
|
429 | |
359 | TYPICAL PROBLEMS |
430 | TYPICAL PROBLEMS |
360 | This section lists typical problems that remain. I hope by recognising |
431 | This section lists typical problems that remain. I hope by recognising |
361 | them, most can be avoided. |
432 | them, most can be avoided. |
362 | |
433 | |
363 | "leaked" file descriptors for exec'ed processes |
434 | "leaked" file descriptors for exec'ed processes |
… | |
… | |
367 | cared, it's often not possible to set the flag in a race-free |
438 | cared, it's often not possible to set the flag in a race-free |
368 | manner. |
439 | manner. |
369 | |
440 | |
370 | That means some file descriptors can leak through. And since it |
441 | That means some file descriptors can leak through. And since it |
371 | isn't possible to know which file descriptors are "good" and |
442 | isn't possible to know which file descriptors are "good" and |
372 | "neccessary" (or even to know which file descreiptors are open), |
443 | "necessary" (or even to know which file descriptors are open), there |
373 | there is no good way to close the ones that might harm. |
444 | is no good way to close the ones that might harm. |
374 | |
445 | |
375 | As an example of what "harm" can be done consider a web server that |
446 | As an example of what "harm" can be done consider a web server that |
376 | accepts connections and afterwards some module uses AnyEvent::Fork |
447 | accepts connections and afterwards some module uses AnyEvent::Fork |
377 | for the first time, causing it to fork and exec a new process, which |
448 | for the first time, causing it to fork and exec a new process, which |
378 | might inherit the network socket. When the server closes the socket, |
449 | might inherit the network socket. When the server closes the socket, |
… | |
… | |
385 | exec'ed well before many random file descriptors are open. |
456 | exec'ed well before many random file descriptors are open. |
386 | |
457 | |
387 | In general, the solution for these kind of problems is to fix the |
458 | In general, the solution for these kind of problems is to fix the |
388 | libraries or the code that leaks those file descriptors. |
459 | libraries or the code that leaks those file descriptors. |
389 | |
460 | |
390 | Fortunately, most of these lekaed descriptors do no harm, other than |
461 | Fortunately, most of these leaked descriptors do no harm, other than |
391 | sitting on some resources. |
462 | sitting on some resources. |
392 | |
463 | |
393 | "leaked" file descriptors for fork'ed processes |
464 | "leaked" file descriptors for fork'ed processes |
394 | Normally, AnyEvent::Fork does start new processes by exec'ing them, |
465 | Normally, AnyEvent::Fork does start new processes by exec'ing them, |
395 | which closes file descriptors not marked for being inherited. |
466 | which closes file descriptors not marked for being inherited. |
… | |
… | |
405 | trouble with a fork. |
476 | trouble with a fork. |
406 | |
477 | |
407 | The solution is to either not load these modules before use'ing |
478 | The solution is to either not load these modules before use'ing |
408 | AnyEvent::Fork::Early or AnyEvent::Fork::Template, or to delay |
479 | AnyEvent::Fork::Early or AnyEvent::Fork::Template, or to delay |
409 | initialising them, for example, by calling "init Gtk2" manually. |
480 | initialising them, for example, by calling "init Gtk2" manually. |
|
|
481 | |
|
|
482 | exit runs destructors |
|
|
483 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
|
|
484 | AnyEvent::Fork::Template. |
|
|
485 | |
|
|
486 | When a process created by AnyEvent::Fork exits, it might do so by |
|
|
487 | calling exit, or simply letting perl reach the end of the program. |
|
|
488 | At which point Perl runs all destructors. |
|
|
489 | |
|
|
490 | Not all destructors are fork-safe - for example, an object that |
|
|
491 | represents the connection to an X display might tell the X server to |
|
|
492 | free resources, which is inconvenient when the "real" object in the |
|
|
493 | parent still needs to use them. |
|
|
494 | |
|
|
495 | This is obviously not a problem for AnyEvent::Fork::Early, as you |
|
|
496 | used it as the very first thing, right? |
|
|
497 | |
|
|
498 | It is a problem for AnyEvent::Fork::Template though - and the |
|
|
499 | solution is to not create objects with nontrivial destructors that |
|
|
500 | might have an effect outside of Perl. |
410 | |
501 | |
411 | PORTABILITY NOTES |
502 | PORTABILITY NOTES |
412 | Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a |
503 | Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a |
413 | nop, and ::Template is not going to work), and it cost a lot of blood |
504 | nop, and ::Template is not going to work), and it cost a lot of blood |
414 | and sweat to make it so, mostly due to the bloody broken perl that |
505 | and sweat to make it so, mostly due to the bloody broken perl that |
415 | nobody seems to care about. The fork emulation is a bad joke - I have |
506 | nobody seems to care about. The fork emulation is a bad joke - I have |
416 | yet to see something useful that you cna do with it without running into |
507 | yet to see something useful that you can do with it without running into |
417 | memory corruption issues or other braindamage. Hrrrr. |
508 | memory corruption issues or other braindamage. Hrrrr. |
418 | |
509 | |
419 | Cygwin perl is not supported at the moment, as it should implement fd |
510 | Cygwin perl is not supported at the moment, as it should implement fd |
420 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
511 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
421 | support enough functionality to do it. |
512 | support enough functionality to do it. |