ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork/README
(Generate patch)

Comparing AnyEvent-Fork/README (file contents):
Revision 1.4 by root, Sat Apr 6 03:42:26 2013 UTC vs.
Revision 1.10 by root, Thu May 12 16:54:01 2016 UTC

2 AnyEvent::Fork - everything you wanted to use fork() for, but couldn't 2 AnyEvent::Fork - everything you wanted to use fork() for, but couldn't
3 3
4SYNOPSIS 4SYNOPSIS
5 use AnyEvent::Fork; 5 use AnyEvent::Fork;
6 6
7 ################################################################## 7 AnyEvent::Fork
8 ->new
9 ->require ("MyModule")
10 ->run ("MyModule::server", my $cv = AE::cv);
11
12 my $fh = $cv->recv;
13
14DESCRIPTION
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 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 And if you need some automatic process pool management on top of
40 AnyEvent::Fork::RPC, you can look at the AnyEvent::Fork::Pool companion
41 module.
42
43 Or you can implement it yourself in whatever way you like: use some
44 message-passing module such as AnyEvent::MP, some pipe such as
45 AnyEvent::ZeroMQ, use AnyEvent::Handle on both sides to send e.g. JSON
46 or Storable messages, and so on.
47
48 COMPARISON TO OTHER MODULES
49 There is an abundance of modules on CPAN that do "something fork", such
50 as Parallel::ForkManager, AnyEvent::ForkManager, AnyEvent::Worker or
51 AnyEvent::Subprocess. There are modules that implement their own process
52 management, such as AnyEvent::DBI.
53
54 The problems that all these modules try to solve are real, however, none
55 of them (from what I have seen) tackle the very real problems of
56 unwanted memory sharing, efficiency or not being able to use event
57 processing, GUI toolkits or similar modules in the processes they
58 create.
59
60 This module doesn't try to replace any of them - instead it tries to
61 solve the problem of creating processes with a minimum of fuss and
62 overhead (and also luxury). Ideally, most of these would use
63 AnyEvent::Fork internally, except they were written before AnyEvent:Fork
64 was available, so obviously had to roll their own.
65
66 PROBLEM STATEMENT
67 There are two traditional ways to implement parallel processing on UNIX
68 like operating systems - fork and process, and fork+exec and process.
69 They have different advantages and disadvantages that I describe below,
70 together with how this module tries to mitigate the disadvantages.
71
72 Forking from a big process can be very slow.
73 A 5GB process needs 0.05s to fork on my 3.6GHz amd64 GNU/Linux box.
74 This overhead is often shared with exec (because you have to fork
75 first), but in some circumstances (e.g. when vfork is used),
76 fork+exec can be much faster.
77
78 This module can help here by telling a small(er) helper process to
79 fork, which is faster then forking the main process, and also uses
80 vfork where possible. This gives the speed of vfork, with the
81 flexibility of fork.
82
83 Forking usually creates a copy-on-write copy of the parent process.
84 For example, modules or data files that are loaded will not use
85 additional memory after a fork. Exec'ing a new process, in contrast,
86 means modules and data files might need to be loaded again, at extra
87 CPU and memory cost.
88
89 But when forking, you still create a copy of your data structures -
90 if the program frees them and replaces them by new data, the child
91 processes will retain the old version even if it isn't used, which
92 can suddenly and unexpectedly increase memory usage when freeing
93 memory.
94
95 For example, Gtk2::CV is an image viewer optimised for large
96 directories (millions of pictures). It also forks subprocesses for
97 thumbnail generation, which inherit the data structure that stores
98 all file information. If the user changes the directory, it gets
99 freed in the main process, leaving a copy in the thumbnailer
100 processes. This can lead to many times the memory usage that would
101 actually be required. The solution is to fork early (and being
102 unable to dynamically generate more subprocesses or do this from a
103 module)... or to use <AnyEvent:Fork>.
104
105 There is a trade-off between more sharing with fork (which can be
106 good or bad), and no sharing with exec.
107
108 This module allows the main program to do a controlled fork, and
109 allows modules to exec processes safely at any time. When creating a
110 custom process pool you can take advantage of data sharing via fork
111 without risking to share large dynamic data structures that will
112 blow up child memory usage.
113
114 In other words, this module puts you into control over what is being
115 shared and what isn't, at all times.
116
117 Exec'ing a new perl process might be difficult.
118 For example, it is not easy to find the correct path to the perl
119 interpreter - $^X might not be a perl interpreter at all. Worse,
120 there might not even be a perl binary installed on the system.
121
122 This module tries hard to identify the correct path to the perl
123 interpreter. With a cooperative main program, exec'ing the
124 interpreter might not even be necessary, but even without help from
125 the main program, it will still work when used from a module.
126
127 Exec'ing a new perl process might be slow, as all necessary modules have
128 to be loaded from disk again, with no guarantees of success.
129 Long running processes might run into problems when perl is upgraded
130 and modules are no longer loadable because they refer to a different
131 perl version, or parts of a distribution are newer than the ones
132 already loaded.
133
134 This module supports creating pre-initialised perl processes to be
135 used as a template for new processes at a later time, e.g. for use
136 in a process pool.
137
138 Forking might be impossible when a program is running.
139 For example, POSIX makes it almost impossible to fork from a
140 multi-threaded program while doing anything useful in the child - in
141 fact, if your perl program uses POSIX threads (even indirectly via
142 e.g. IO::AIO or threads), you cannot call fork on the perl level
143 anymore without risking memory corruption or worse on a number of
144 operating systems.
145
146 This module can safely fork helper processes at any time, by calling
147 fork+exec in C, in a POSIX-compatible way (via Proc::FastSpawn).
148
149 Parallel processing with fork might be inconvenient or difficult to
150 implement. Modules might not work in both parent and child.
151 For example, when a program uses an event loop and creates watchers
152 it becomes very hard to use the event loop from a child program, as
153 the watchers already exist but are only meaningful in the parent.
154 Worse, a module might want to use such a module, not knowing whether
155 another module or the main program also does, leading to problems.
156
157 Apart from event loops, graphical toolkits also commonly fall into
158 the "unsafe module" category, or just about anything that
159 communicates with the external world, such as network libraries and
160 file I/O modules, which usually don't like being copied and then
161 allowed to continue in two processes.
162
163 With this module only the main program is allowed to create new
164 processes by forking (because only the main program can know when it
165 is still safe to do so) - all other processes are created via
166 fork+exec, which makes it possible to use modules such as event
167 loops or window interfaces safely.
168
169EXAMPLES
170 This is where the wall of text ends and code speaks.
171
8 # create a single new process, tell it to run your worker function 172 Create a single new process, tell it to run your worker function.
9
10 AnyEvent::Fork 173 AnyEvent::Fork
11 ->new 174 ->new
12 ->require ("MyModule") 175 ->require ("MyModule")
13 ->run ("MyModule::worker, sub { 176 ->run ("MyModule::worker, sub {
14 my ($master_filehandle) = @_; 177 my ($master_filehandle) = @_;
15 178
16 # now $master_filehandle is connected to the 179 # now $master_filehandle is connected to the
17 # $slave_filehandle in the new process. 180 # $slave_filehandle in the new process.
18 }); 181 });
19 182
20 # MyModule::worker might look like this 183 "MyModule" might look like this:
184
185 package MyModule;
186
21 sub MyModule::worker { 187 sub worker {
22 my ($slave_filehandle) = @_; 188 my ($slave_filehandle) = @_;
23 189
24 # now $slave_filehandle is connected to the $master_filehandle 190 # now $slave_filehandle is connected to the $master_filehandle
25 # in the original prorcess. have fun! 191 # in the original process. have fun!
26 } 192 }
27 193
28 ##################################################################
29 # create a pool of server processes all accepting on the same socket 194 Create a pool of server processes all accepting on the same socket.
30
31 # create listener socket 195 # create listener socket
32 my $listener = ...; 196 my $listener = ...;
33 197
34 # create a pool template, initialise it and give it the socket 198 # create a pool template, initialise it and give it the socket
35 my $pool = AnyEvent::Fork 199 my $pool = AnyEvent::Fork
46 } 210 }
47 211
48 # now do other things - maybe use the filehandle provided by run 212 # now do other things - maybe use the filehandle provided by run
49 # to wait for the processes to die. or whatever. 213 # to wait for the processes to die. or whatever.
50 214
51 # My::Server::run might look like this 215 "My::Server" might look like this:
52 sub My::Server::run { 216
217 package My::Server;
218
219 sub run {
53 my ($slave, $listener, $id) = @_; 220 my ($slave, $listener, $id) = @_;
54 221
55 close $slave; # we do not use the socket, so close it to save resources 222 close $slave; # we do not use the socket, so close it to save resources
56 223
57 # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, 224 # we could go ballistic and use e.g. AnyEvent here, or IO::AIO,
59 while (my $socket = $listener->accept) { 226 while (my $socket = $listener->accept) {
60 # do sth. with new socket 227 # do sth. with new socket
61 } 228 }
62 } 229 }
63 230
64DESCRIPTION 231 use AnyEvent::Fork as a faster fork+exec
65 This module allows you to create new processes, without actually forking 232 This runs "/bin/echo hi", with standard output redirected to /tmp/log
66 them from your current process (avoiding the problems of forking), but 233 and standard error redirected to the communications socket. It is
67 preserving most of the advantages of fork. 234 usually faster than fork+exec, but still lets you prepare the
235 environment.
68 236
69 It can be used to create new worker processes or new independent 237 open my $output, ">/tmp/log" or die "$!";
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
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.
74 238
75 Special care has been taken to make this module useful from other 239 AnyEvent::Fork
76 modules, while still supporting specialised environments such as 240 ->new
241 ->eval ('
242 # compile a helper function for later use
243 sub run {
244 my ($fh, $output, @cmd) = @_;
245
246 # perl will clear close-on-exec on STDOUT/STDERR
247 open STDOUT, ">&", $output or die;
248 open STDERR, ">&", $fh or die;
249
250 exec @cmd;
251 }
252 ')
253 ->send_fh ($output)
254 ->send_arg ("/bin/echo", "hi")
255 ->run ("run", my $cv = AE::cv);
256
257 my $stderr = $cv->recv;
258
259 For stingy users: put the worker code into a "DATA" section.
260 When you want to be stingy with files, you can put your code into the
261 "DATA" section of your module (or program):
262
263 use AnyEvent::Fork;
264
265 AnyEvent::Fork
266 ->new
267 ->eval (do { local $/; <DATA> })
268 ->run ("doit", sub { ... });
269
270 __DATA__
271
272 sub doit {
273 ... do something!
274 }
275
276 For stingy standalone programs: do not rely on external files at
277all.
278 For single-file scripts it can be inconvenient to rely on external files
279 - even when using a "DATA" section, you still need to "exec" an external
280 perl interpreter, which might not be available when using
77 App::Staticperl or PAR::Packer. 281 App::Staticperl, Urlader or PAR::Packer for example.
78 282
79WHAT THIS MODULE IS NOT 283 Two modules help here - AnyEvent::Fork::Early forks a template process
80 This module only creates processes and lets you pass file handles and 284 for all further calls to "new_exec", and AnyEvent::Fork::Template forks
81 strings to it, and run perl code. It does not implement any kind of RPC 285 the main program as a template process.
82 - there is no back channel from the process back to you, and there is no
83 RPC or message passing going on.
84 286
85 If you need some form of RPC, you can either implement it yourself in 287 Here is how your main program should look like:
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.
89 288
90PROBLEM STATEMENT 289 #! perl
91 There are two ways to implement parallel processing on UNIX like
92 operating systems - fork and process, and fork+exec and process. They
93 have different advantages and disadvantages that I describe below,
94 together with how this module tries to mitigate the disadvantages.
95 290
96 Forking from a big process can be very slow (a 5GB process needs 0.05s 291 # optional, as the very first thing.
97 to fork on my 3.6GHz amd64 GNU/Linux box for example). This overhead is 292 # in case modules want to create their own processes.
98 often shared with exec (because you have to fork first), but in some 293 use AnyEvent::Fork::Early;
99 circumstances (e.g. when vfork is used), fork+exec can be much faster.
100 This module can help here by telling a small(er) helper process to
101 fork, or fork+exec instead.
102 294
103 Forking usually creates a copy-on-write copy of the parent process. 295 # next, load all modules you need in your template process
104 Memory (for example, modules or data files that have been will not take 296 use Example::My::Module
105 additional memory). When exec'ing a new process, modules and data files 297 use Example::Whatever;
106 might need to be loaded again, at extra CPU and memory cost. Likewise
107 when forking, all data structures are copied as well - if the program
108 frees them and replaces them by new data, the child processes will
109 retain the memory even if it isn't used.
110 This module allows the main program to do a controlled fork, and
111 allows modules to exec processes safely at any time. When creating a
112 custom process pool you can take advantage of data sharing via fork
113 without risking to share large dynamic data structures that will
114 blow up child memory usage.
115 298
116 Exec'ing a new perl process might be difficult and slow. For example, it 299 # next, put your run function definition and anything else you
117 is not easy to find the correct path to the perl interpreter, and all 300 # need, but do not use code outside of BEGIN blocks.
118 modules have to be loaded from disk again. Long running processes might 301 sub worker_run {
119 run into problems when perl is upgraded for example. 302 my ($fh, @args) = @_;
120 This module supports creating pre-initialised perl processes to be 303 ...
121 used as template, and also tries hard to identify the correct path 304 }
122 to the perl interpreter. With a cooperative main program, exec'ing
123 the interpreter might not even be necessary.
124 305
125 Forking might be impossible when a program is running. For example, 306 # now preserve everything so far as AnyEvent::Fork object
126 POSIX makes it almost impossible to fork from a multi-threaded program 307 # in $TEMPLATE.
127 and do anything useful in the child - strictly speaking, if your perl 308 use AnyEvent::Fork::Template;
128 program uses posix threads (even indirectly via e.g. IO::AIO or
129 threads), you cannot call fork on the perl level anymore, at all.
130 This module can safely fork helper processes at any time, by calling
131 fork+exec in C, in a POSIX-compatible way.
132 309
133 Parallel processing with fork might be inconvenient or difficult to 310 # do not put code outside of BEGIN blocks until here
134 implement. For example, when a program uses an event loop and creates 311
135 watchers it becomes very hard to use the event loop from a child 312 # now use the $TEMPLATE process in any way you like
136 program, as the watchers already exist but are only meaningful in the 313
137 parent. Worse, a module might want to use such a system, not knowing 314 # for example: create 10 worker processes
138 whether another module or the main program also does, leading to 315 my @worker;
139 problems. 316 my $cv = AE::cv;
140 This module only lets the main program create pools by forking 317 for (1..10) {
141 (because only the main program can know when it is still safe to do 318 $cv->begin;
142 so) - all other pools are created by fork+exec, after which such 319 $TEMPLATE->fork->send_arg ($_)->run ("worker_run", sub {
143 modules can again be loaded. 320 push @worker, shift;
321 $cv->end;
322 });
323 }
324 $cv->recv;
144 325
145CONCEPTS 326CONCEPTS
146 This module can create new processes either by executing a new perl 327 This module can create new processes either by executing a new perl
147 process, or by forking from an existing "template" process. 328 process, or by forking from an existing "template" process.
329
330 All these processes are called "child processes" (whether they are
331 direct children or not), while the process that manages them is called
332 the "parent process".
148 333
149 Each such process comes with its own file handle that can be used to 334 Each such process comes with its own file handle that can be used to
150 communicate with it (it's actually a socket - one end in the new 335 communicate with it (it's actually a socket - one end in the new
151 process, one end in the main process), and among the things you can do 336 process, one end in the main process), and among the things you can do
152 in it are load modules, fork new processes, send file handles to it, and 337 in it are load modules, fork new processes, send file handles to it, and
220 ->require ("Some::Module") 405 ->require ("Some::Module")
221 ->run ("Some::Module::run", sub { 406 ->run ("Some::Module::run", sub {
222 my ($fork_fh) = @_; 407 my ($fork_fh) = @_;
223 }); 408 });
224 409
225FUNCTIONS 410THE "AnyEvent::Fork" CLASS
226 my $pool = new AnyEvent::Fork key => value... 411 This module exports nothing, and only implements a single class -
227 Create a new process pool. The following named parameters are 412 "AnyEvent::Fork".
228 supported: 413
414 There are two class constructors that both create new processes - "new"
415 and "new_exec". The "fork" method creates a new process by forking an
416 existing one and could be considered a third constructor.
417
418 Most of the remaining methods deal with preparing the new process, by
419 loading code, evaluating code and sending data to the new process. They
420 usually return the process object, so you can chain method calls.
421
422 If a process object is destroyed before calling its "run" method, then
423 the process simply exits. After "run" is called, all responsibility is
424 passed to the specified function.
425
426 As long as there is any outstanding work to be done, process objects
427 resist being destroyed, so there is no reason to store them unless you
428 need them later - configure and forget works just fine.
229 429
230 my $proc = new AnyEvent::Fork 430 my $proc = new AnyEvent::Fork
231 Create a new "empty" perl interpreter process and returns its 431 Create a new "empty" perl interpreter process and returns its
232 process object for further manipulation. 432 process object for further manipulation.
233 433
234 The new process is forked from a template process that is kept 434 The new process is forked from a template process that is kept
235 around for this purpose. When it doesn't exist yet, it is created by 435 around for this purpose. When it doesn't exist yet, it is created by
236 a call to "new_exec" and kept around for future calls. 436 a call to "new_exec" first and then stays around for future calls.
237
238 When the process object is destroyed, it will release the file
239 handle that connects it with the new process. When the new process
240 has not yet called "run", then the process will exit. Otherwise,
241 what happens depends entirely on the code that is executed.
242 437
243 $new_proc = $proc->fork 438 $new_proc = $proc->fork
244 Forks $proc, creating a new process, and returns the process object 439 Forks $proc, creating a new process, and returns the process object
245 of the new process. 440 of the new process.
246 441
261 You should use "new" whenever possible, except when having a 456 You should use "new" whenever possible, except when having a
262 template process around is unacceptable. 457 template process around is unacceptable.
263 458
264 The path to the perl interpreter is divined using various methods - 459 The path to the perl interpreter is divined using various methods -
265 first $^X is investigated to see if the path ends with something 460 first $^X is investigated to see if the path ends with something
266 that sounds as if it were the perl interpreter. Failing this, the 461 that looks as if it were the perl interpreter. Failing this, the
267 module falls back to using $Config::Config{perlpath}. 462 module falls back to using $Config::Config{perlpath}.
463
464 The path to perl can also be overriden by setting the global
465 variable $AnyEvent::Fork::PERL - it's value will be used for all
466 subsequent invocations.
268 467
269 $pid = $proc->pid 468 $pid = $proc->pid
270 Returns the process id of the process *iff it is a direct child of 469 Returns the process id of the process *iff it is a direct child of
271 the process* running AnyEvent::Fork, and "undef" otherwise. 470 the process running AnyEvent::Fork*, and "undef" otherwise. As a
471 general rule (that you cannot rely upon), processes created via
472 "new_exec", AnyEvent::Fork::Early or AnyEvent::Fork::Template are
473 direct children, while all other processes are not.
272 474
273 Normally, only processes created via "AnyEvent::Fork->new_exec" and 475 Or in other words, you do not normally have to take care of zombies
274 AnyEvent::Fork::Template are direct children, and you are 476 for processes created via "new", but when in doubt, or zombies are a
275 responsible to clean up their zombies when they die. 477 problem, you need to check whether a process is a diretc child by
276 478 calling this method, and possibly creating a child watcher or reap
277 All other processes are not direct children, and will be cleaned up 479 it manually.
278 by AnyEvent::Fork.
279 480
280 $proc = $proc->eval ($perlcode, @args) 481 $proc = $proc->eval ($perlcode, @args)
281 Evaluates the given $perlcode as ... perl code, while setting @_ to 482 Evaluates the given $perlcode as ... Perl code, while setting @_ to
282 the strings specified by @args. 483 the strings specified by @args, in the "main" package.
283 484
284 This call is meant to do any custom initialisation that might be 485 This call is meant to do any custom initialisation that might be
285 required (for example, the "require" method uses it). It's not 486 required (for example, the "require" method uses it). It's not
286 supposed to be used to completely take over the process, use "run" 487 supposed to be used to completely take over the process, use "run"
287 for that. 488 for that.
289 The code will usually be executed after this call returns, and there 490 The code will usually be executed after this call returns, and there
290 is no way to pass anything back to the calling process. Any 491 is no way to pass anything back to the calling process. Any
291 evaluation errors will be reported to stderr and cause the process 492 evaluation errors will be reported to stderr and cause the process
292 to exit. 493 to exit.
293 494
495 If you want to execute some code (that isn't in a module) to take
496 over the process, you should compile a function via "eval" first,
497 and then call it via "run". This also gives you access to any
498 arguments passed via the "send_xxx" methods, such as file handles.
499 See the "use AnyEvent::Fork as a faster fork+exec" example to see it
500 in action.
501
294 Returns the process object for easy chaining of method calls. 502 Returns the process object for easy chaining of method calls.
503
504 It's common to want to call an iniitalisation function with some
505 arguments. Make sure you actually pass @_ to that function (for
506 example by using &name syntax), and do not just specify a function
507 name:
508
509 $proc->eval ('&MyModule::init', $string1, $string2);
295 510
296 $proc = $proc->require ($module, ...) 511 $proc = $proc->require ($module, ...)
297 Tries to load the given module(s) into the process 512 Tries to load the given module(s) into the process
298 513
299 Returns the process object for easy chaining of method calls. 514 Returns the process object for easy chaining of method calls.
300 515
301 $proc = $proc->send_fh ($handle, ...) 516 $proc = $proc->send_fh ($handle, ...)
302 Send one or more file handles (*not* file descriptors) to the 517 Send one or more file handles (*not* file descriptors) to the
303 process, to prepare a call to "run". 518 process, to prepare a call to "run".
304 519
305 The process object keeps a reference to the handles until this is 520 The process object keeps a reference to the handles until they have
306 done, so you must not explicitly close the handles. This is most 521 been passed over to the process, so you must not explicitly close
307 easily accomplished by simply not storing the file handles anywhere 522 the handles. This is most easily accomplished by simply not storing
308 after passing them to this method. 523 the file handles anywhere after passing them to this method - when
524 AnyEvent::Fork is finished using them, perl will automatically close
525 them.
309 526
310 Returns the process object for easy chaining of method calls. 527 Returns the process object for easy chaining of method calls.
311 528
312 Example: pass a file handle to a process, and release it without 529 Example: pass a file handle to a process, and release it without
313 closing. It will be closed automatically when it is no longer used. 530 closing. It will be closed automatically when it is no longer used.
315 $proc->send_fh ($my_fh); 532 $proc->send_fh ($my_fh);
316 undef $my_fh; # free the reference if you want, but DO NOT CLOSE IT 533 undef $my_fh; # free the reference if you want, but DO NOT CLOSE IT
317 534
318 $proc = $proc->send_arg ($string, ...) 535 $proc = $proc->send_arg ($string, ...)
319 Send one or more argument strings to the process, to prepare a call 536 Send one or more argument strings to the process, to prepare a call
320 to "run". The strings can be any octet string. 537 to "run". The strings can be any octet strings.
321 538
322 The protocol is optimised to pass a moderate number of relatively 539 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 540 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 541 is more meant to pass some ID information or other startup info, not
325 big chunks of data. 542 big chunks of data.
326 543
327 Returns the process object for easy chaining of method calls. 544 Returns the process object for easy chaining of method calls.
328 545
329 $proc->run ($func, $cb->($fh)) 546 $proc->run ($func, $cb->($fh))
330 Enter the function specified by the fully qualified name in $func in 547 Enter the function specified by the function name in $func in the
331 the process. The function is called with the communication socket as 548 process. The function is called with the communication socket as
332 first argument, followed by all file handles and string arguments 549 first argument, followed by all file handles and string arguments
333 sent earlier via "send_fh" and "send_arg" methods, in the order they 550 sent earlier via "send_fh" and "send_arg" methods, in the order they
334 were called. 551 were called.
335 552
336 If the called function returns, the process exits.
337
338 Preparing the process can take time - when the process is ready, the
339 callback is invoked with the local communications socket as
340 argument.
341
342 The process object becomes unusable on return from this function. 553 The process object becomes unusable on return from this function -
554 any further method calls result in undefined behaviour.
555
556 The function name should be fully qualified, but if it isn't, it
557 will be looked up in the "main" package.
558
559 If the called function returns, doesn't exist, or any error occurs,
560 the process exits.
561
562 Preparing the process is done in the background - when all commands
563 have been sent, the callback is invoked with the local
564 communications socket as argument. At this point you can start using
565 the socket in any way you like.
343 566
344 If the communication socket isn't used, it should be closed on both 567 If the communication socket isn't used, it should be closed on both
345 sides, to save on kernel memory. 568 sides, to save on kernel memory.
346 569
347 The socket is non-blocking in the parent, and blocking in the newly 570 The socket is non-blocking in the parent, and blocking in the newly
348 created process. The close-on-exec flag is set on both. Even if not 571 created process. The close-on-exec flag is set in both.
572
349 used otherwise, the socket can be a good indicator for the existence 573 Even if not used otherwise, the socket can be a good indicator for
350 of the process - if the other process exits, you get a readable 574 the existence of the process - if the other process exits, you get a
351 event on it, because exiting the process closes the socket (if it 575 readable event on it, because exiting the process closes the socket
352 didn't create any children using fork). 576 (if it didn't create any children using fork).
577
578 Compatibility to AnyEvent::Fork::Remote
579 If you want to write code that works with both this module and
580 AnyEvent::Fork::Remote, you need to write your code so that it
581 assumes there are two file handles for communications, which
582 might not be unix domain sockets. The "run" function should
583 start like this:
584
585 sub run {
586 my ($rfh, @args) = @_; # @args is your normal arguments
587 my $wfh = fileno $rfh ? $rfh : *STDOUT;
588
589 # now use $rfh for reading and $wfh for writing
590 }
591
592 This checks whether the passed file handle is, in fact, the
593 process "STDIN" handle. If it is, then the function was invoked
594 visa AnyEvent::Fork::Remote, so STDIN should be used for reading
595 and "STDOUT" should be used for writing.
596
597 In all other cases, the function was called via this module, and
598 there is only one file handle that should be sued for reading
599 and writing.
353 600
354 Example: create a template for a process pool, pass a few strings, 601 Example: create a template for a process pool, pass a few strings,
355 some file handles, then fork, pass one more string, and run some 602 some file handles, then fork, pass one more string, and run some
356 code. 603 code.
357 604
366 ->send_arg ("str3") 613 ->send_arg ("str3")
367 ->run ("Some::function", sub { 614 ->run ("Some::function", sub {
368 my ($fh) = @_; 615 my ($fh) = @_;
369 616
370 # fh is nonblocking, but we trust that the OS can accept these 617 # fh is nonblocking, but we trust that the OS can accept these
371 # extra 3 octets anyway. 618 # few octets anyway.
372 syswrite $fh, "hi #$_\n"; 619 syswrite $fh, "hi #$_\n";
373 620
374 # $fh is being closed here, as we don't store it anywhere 621 # $fh is being closed here, as we don't store it anywhere
375 }); 622 });
376 } 623 }
378 # Some::function might look like this - all parameters passed before fork 625 # Some::function might look like this - all parameters passed before fork
379 # and after will be passed, in order, after the communications socket. 626 # and after will be passed, in order, after the communications socket.
380 sub Some::function { 627 sub Some::function {
381 my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; 628 my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_;
382 629
383 print scalar <$fh>; # prints "hi 1\n" and "hi 2\n" 630 print scalar <$fh>; # prints "hi #1\n" and "hi #2\n" in any order
384 } 631 }
632
633 EXPERIMENTAL METHODS
634 These methods might go away completely or change behaviour, at any time.
635
636 $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED
637 Flushes all commands out to the process and then calls the callback
638 with the communications socket.
639
640 The process object becomes unusable on return from this function -
641 any further method calls result in undefined behaviour.
642
643 The point of this method is to give you a file handle that you can
644 pass to another process. In that other process, you can call
645 "new_from_fh AnyEvent::Fork $fh" to create a new "AnyEvent::Fork"
646 object from it, thereby effectively passing a fork object to another
647 process.
648
649 new_from_fh AnyEvent::Fork $fh # EXPERIMENTAL, MIGHT BE REMOVED
650 Takes a file handle originally rceeived by the "to_fh" method and
651 creates a new "AnyEvent:Fork" object. The child process itself will
652 not change in any way, i.e. it will keep all the modifications done
653 to it before calling "to_fh".
654
655 The new object is very much like the original object, except that
656 the "pid" method will return "undef" even if the process is a direct
657 child.
385 658
386PERFORMANCE 659PERFORMANCE
387 Now for some unscientific benchmark numbers (all done on an amd64 660 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 661 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 662 performance you can expect, they are not meant to be absolute
396 669
397 2079 new processes per second, using manual socketpair + fork 670 2079 new processes per second, using manual socketpair + fork
398 671
399 Then I did the same thing, but instead of calling fork, I called 672 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 673 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 674 socket from 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 675 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 676 process (2440kB), and the socket needs to be passed to the server at the
404 other end of the socket first. 677 other end of the socket first.
405 678
406 2307 new processes per second, using AnyEvent::Fork->new 679 2307 new processes per second, using AnyEvent::Fork->new
411 479 vfork+execs per second, using AnyEvent::Fork->new_exec 684 479 vfork+execs per second, using AnyEvent::Fork->new_exec
412 685
413 So how can "AnyEvent->new" be faster than a standard fork, even though 686 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? 687 it uses the same operations, but adds a lot of overhead?
415 688
416 The difference is simply the process size: forking the 6MB process takes 689 The difference is simply the process size: forking the 5MB process takes
417 so much longer than forking the 2.5MB template process that the overhead 690 so much longer than forking the 2.5MB template process that the extra
418 introduced is canceled out. 691 overhead is canceled out.
419 692
420 If the benchmark process grows, the normal fork becomes even slower: 693 If the benchmark process grows, the normal fork becomes even slower:
421 694
422 1340 new processes, manual fork in a 20MB process 695 1340 new processes, manual fork of a 20MB process
423 731 new processes, manual fork in a 200MB process 696 731 new processes, manual fork of a 200MB process
424 235 new processes, manual fork in a 2000MB process 697 235 new processes, manual fork of a 2000MB process
425 698
426 What that means (to me) is that I can use this module without having a 699 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 700 bad conscience because of the extra overhead required to start new
428 processes. 701 processes.
429 702
430TYPICAL PROBLEMS 703TYPICAL PROBLEMS
431 This section lists typical problems that remain. I hope by recognising 704 This section lists typical problems that remain. I hope by recognising
432 them, most can be avoided. 705 them, most can be avoided.
433 706
434 "leaked" file descriptors for exec'ed processes 707 leaked file descriptors for exec'ed processes
435 POSIX systems inherit file descriptors by default when exec'ing a 708 POSIX systems inherit file descriptors by default when exec'ing a
436 new process. While perl itself laudably sets the close-on-exec flags 709 new process. While perl itself laudably sets the close-on-exec flags
437 on new file handles, most C libraries don't care, and even if all 710 on new file handles, most C libraries don't care, and even if all
438 cared, it's often not possible to set the flag in a race-free 711 cared, it's often not possible to set the flag in a race-free
439 manner. 712 manner.
459 libraries or the code that leaks those file descriptors. 732 libraries or the code that leaks those file descriptors.
460 733
461 Fortunately, most of these leaked descriptors do no harm, other than 734 Fortunately, most of these leaked descriptors do no harm, other than
462 sitting on some resources. 735 sitting on some resources.
463 736
464 "leaked" file descriptors for fork'ed processes 737 leaked file descriptors for fork'ed processes
465 Normally, AnyEvent::Fork does start new processes by exec'ing them, 738 Normally, AnyEvent::Fork does start new processes by exec'ing them,
466 which closes file descriptors not marked for being inherited. 739 which closes file descriptors not marked for being inherited.
467 740
468 However, AnyEvent::Fork::Early and AnyEvent::Fork::Template offer a 741 However, AnyEvent::Fork::Early and AnyEvent::Fork::Template offer a
469 way to create these processes by forking, and this leaks more file 742 way to create these processes by forking, and this leaks more file
477 750
478 The solution is to either not load these modules before use'ing 751 The solution is to either not load these modules before use'ing
479 AnyEvent::Fork::Early or AnyEvent::Fork::Template, or to delay 752 AnyEvent::Fork::Early or AnyEvent::Fork::Template, or to delay
480 initialising them, for example, by calling "init Gtk2" manually. 753 initialising them, for example, by calling "init Gtk2" manually.
481 754
482 exit runs destructors 755 exiting calls object destructors
483 This only applies to users of Lc<AnyEvent::Fork:Early> and 756 This only applies to users of AnyEvent::Fork:Early and
484 AnyEvent::Fork::Template. 757 AnyEvent::Fork::Template, or when initialising code creates objects
758 that reference external resources.
485 759
486 When a process created by AnyEvent::Fork exits, it might do so by 760 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. 761 calling exit, or simply letting perl reach the end of the program.
488 At which point Perl runs all destructors. 762 At which point Perl runs all destructors.
489 763
505 and sweat to make it so, mostly due to the bloody broken perl that 779 and sweat to make it so, mostly due to the bloody broken perl that
506 nobody seems to care about. The fork emulation is a bad joke - I have 780 nobody seems to care about. The fork emulation is a bad joke - I have
507 yet to see something useful that you can do with it without running into 781 yet to see something useful that you can do with it without running into
508 memory corruption issues or other braindamage. Hrrrr. 782 memory corruption issues or other braindamage. Hrrrr.
509 783
510 Cygwin perl is not supported at the moment, as it should implement fd 784 Since fork is endlessly broken on win32 perls (it doesn't even remotely
511 passing, but doesn't, and rolling my own is hard, as cygwin doesn't 785 work within it's documented limits) and quite obviously it's not getting
512 support enough functionality to do it. 786 improved any time soon, the best way to proceed on windows would be to
787 always use "new_exec" and thus never rely on perl's fork "emulation".
788
789 Cygwin perl is not supported at the moment due to some hilarious
790 shortcomings of its API - see IO::FDPoll for more details. If you never
791 use "send_fh" and always use "new_exec" to create processes, it should
792 work though.
793
794USING AnyEvent::Fork IN SUBPROCESSES
795 AnyEvent::Fork itself cannot generally be used in subprocesses. As long
796 as only one process ever forks new processes, sharing the template
797 processes is possible (you could use a pipe as a lock by writing a byte
798 into it to unlock, and reading the byte to lock for example)
799
800 To make concurrent calls possible after fork, you should get rid of the
801 template and early fork processes. AnyEvent::Fork will create a new
802 template process as needed.
803
804 undef $AnyEvent::Fork::EARLY;
805 undef $AnyEvent::Fork::TEMPLATE;
806
807 It doesn't matter whether you get rid of them in the parent or child
808 after a fork.
513 809
514SEE ALSO 810SEE ALSO
515 AnyEvent::Fork::Early (to avoid executing a perl interpreter), 811 AnyEvent::Fork::Early, to avoid executing a perl interpreter at all
812 (part of this distribution).
813
516 AnyEvent::Fork::Template (to create a process by forking the main 814 AnyEvent::Fork::Template, to create a process by forking the main
517 program at a convenient time). 815 program at a convenient time (part of this distribution).
518 816
519AUTHOR 817 AnyEvent::Fork::Remote, for another way to create processes that is
818 mostly compatible to this module and modules building on top of it, but
819 works better with remote processes.
820
821 AnyEvent::Fork::RPC, for simple RPC to child processes (on CPAN).
822
823 AnyEvent::Fork::Pool, for simple worker process pool (on CPAN).
824
825AUTHOR AND CONTACT INFORMATION
520 Marc Lehmann <schmorp@schmorp.de> 826 Marc Lehmann <schmorp@schmorp.de>
521 http://home.schmorp.de/ 827 http://software.schmorp.de/pkg/AnyEvent-Fork
522 828

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines