… | |
… | |
40 | in whatever way you like, use some message-passing module such |
40 | in whatever way you like, use some message-passing module such |
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
41 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
42 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
43 | and so on. |
43 | and so on. |
44 | |
44 | |
|
|
45 | =head1 PROBLEM STATEMENT |
|
|
46 | |
|
|
47 | There are two traditional ways to implement parallel processing on UNIX |
|
|
48 | like operating systems - fork and process, and fork+exec and process. They |
|
|
49 | have different advantages and disadvantages that I describe below, |
|
|
50 | together with how this module tries to mitigate the disadvantages. |
|
|
51 | |
|
|
52 | =over 4 |
|
|
53 | |
|
|
54 | =item Forking from a big process can be very slow. |
|
|
55 | |
|
|
56 | A 5GB process needs 0.05s to fork on my 3.6GHz amd64 GNU/Linux box. This |
|
|
57 | overhead is often shared with exec (because you have to fork first), but |
|
|
58 | in some circumstances (e.g. when vfork is used), fork+exec can be much |
|
|
59 | faster. |
|
|
60 | |
|
|
61 | This module can help here by telling a small(er) helper process to fork, |
|
|
62 | which is faster then forking the main process, and also uses vfork where |
|
|
63 | possible. This gives the speed of vfork, with the flexibility of fork. |
|
|
64 | |
|
|
65 | =item Forking usually creates a copy-on-write copy of the parent |
|
|
66 | process. |
|
|
67 | |
|
|
68 | For example, modules or data files that are loaded will not use additional |
|
|
69 | memory after a fork. When exec'ing a new process, modules and data files |
|
|
70 | might need to be loaded again, at extra CPU and memory cost. But when |
|
|
71 | forking, literally all data structures are copied - if the program frees |
|
|
72 | them and replaces them by new data, the child processes will retain the |
|
|
73 | old version even if it isn't used, which can suddenly and unexpectedly |
|
|
74 | increase memory usage when freeing memory. |
|
|
75 | |
|
|
76 | The trade-off is between more sharing with fork (which can be good or |
|
|
77 | bad), and no sharing with exec. |
|
|
78 | |
|
|
79 | This module allows the main program to do a controlled fork, and allows |
|
|
80 | modules to exec processes safely at any time. When creating a custom |
|
|
81 | process pool you can take advantage of data sharing via fork without |
|
|
82 | risking to share large dynamic data structures that will blow up child |
|
|
83 | memory usage. |
|
|
84 | |
|
|
85 | In other words, this module puts you into control over what is being |
|
|
86 | shared and what isn't, at all times. |
|
|
87 | |
|
|
88 | =item Exec'ing a new perl process might be difficult. |
|
|
89 | |
|
|
90 | For example, it is not easy to find the correct path to the perl |
|
|
91 | interpreter - C<$^X> might not be a perl interpreter at all. |
|
|
92 | |
|
|
93 | This module tries hard to identify the correct path to the perl |
|
|
94 | interpreter. With a cooperative main program, exec'ing the interpreter |
|
|
95 | might not even be necessary, but even without help from the main program, |
|
|
96 | it will still work when used from a module. |
|
|
97 | |
|
|
98 | =item Exec'ing a new perl process might be slow, as all necessary modules |
|
|
99 | have to be loaded from disk again, with no guarantees of success. |
|
|
100 | |
|
|
101 | Long running processes might run into problems when perl is upgraded |
|
|
102 | and modules are no longer loadable because they refer to a different |
|
|
103 | perl version, or parts of a distribution are newer than the ones already |
|
|
104 | loaded. |
|
|
105 | |
|
|
106 | This module supports creating pre-initialised perl processes to be used as |
|
|
107 | a template for new processes. |
|
|
108 | |
|
|
109 | =item Forking might be impossible when a program is running. |
|
|
110 | |
|
|
111 | For example, POSIX makes it almost impossible to fork from a |
|
|
112 | multi-threaded program while doing anything useful in the child - in |
|
|
113 | fact, if your perl program uses POSIX threads (even indirectly via |
|
|
114 | e.g. L<IO::AIO> or L<threads>), you cannot call fork on the perl level |
|
|
115 | anymore without risking corruption issues on a number of operating |
|
|
116 | systems. |
|
|
117 | |
|
|
118 | This module can safely fork helper processes at any time, by calling |
|
|
119 | fork+exec in C, in a POSIX-compatible way (via L<Proc::FastSpawn>). |
|
|
120 | |
|
|
121 | =item Parallel processing with fork might be inconvenient or difficult |
|
|
122 | to implement. Modules might not work in both parent and child. |
|
|
123 | |
|
|
124 | For example, when a program uses an event loop and creates watchers it |
|
|
125 | becomes very hard to use the event loop from a child program, as the |
|
|
126 | watchers already exist but are only meaningful in the parent. Worse, a |
|
|
127 | module might want to use such a module, not knowing whether another module |
|
|
128 | or the main program also does, leading to problems. |
|
|
129 | |
|
|
130 | Apart from event loops, graphical toolkits also commonly fall into the |
|
|
131 | "unsafe module" category, or just about anything that communicates with |
|
|
132 | the external world, such as network libraries and file I/O modules, which |
|
|
133 | usually don't like being copied and then allowed to continue in two |
|
|
134 | processes. |
|
|
135 | |
|
|
136 | With this module only the main program is allowed to create new processes |
|
|
137 | by forking (because only the main program can know when it is still safe |
|
|
138 | to do so) - all other processes are created via fork+exec, which makes it |
|
|
139 | possible to use modules such as event loops or window interfaces safely. |
|
|
140 | |
|
|
141 | =back |
|
|
142 | |
45 | =head1 EXAMPLES |
143 | =head1 EXAMPLES |
46 | |
144 | |
47 | =head2 Create a single new process, tell it to run your worker function. |
145 | =head2 Create a single new process, tell it to run your worker function. |
48 | |
146 | |
49 | AnyEvent::Fork |
147 | AnyEvent::Fork |
… | |
… | |
54 | |
152 | |
55 | # now $master_filehandle is connected to the |
153 | # now $master_filehandle is connected to the |
56 | # $slave_filehandle in the new process. |
154 | # $slave_filehandle in the new process. |
57 | }); |
155 | }); |
58 | |
156 | |
59 | # MyModule::worker might look like this |
157 | C<MyModule> might look like this: |
|
|
158 | |
|
|
159 | package MyModule; |
|
|
160 | |
60 | sub MyModule::worker { |
161 | sub worker { |
61 | my ($slave_filehandle) = @_; |
162 | my ($slave_filehandle) = @_; |
62 | |
163 | |
63 | # now $slave_filehandle is connected to the $master_filehandle |
164 | # now $slave_filehandle is connected to the $master_filehandle |
64 | # in the original prorcess. have fun! |
165 | # in the original prorcess. have fun! |
65 | } |
166 | } |
… | |
… | |
84 | } |
185 | } |
85 | |
186 | |
86 | # now do other things - maybe use the filehandle provided by run |
187 | # now do other things - maybe use the filehandle provided by run |
87 | # to wait for the processes to die. or whatever. |
188 | # to wait for the processes to die. or whatever. |
88 | |
189 | |
89 | # My::Server::run might look like this |
190 | C<My::Server> might look like this: |
90 | sub My::Server::run { |
191 | |
|
|
192 | package My::Server; |
|
|
193 | |
|
|
194 | sub run { |
91 | my ($slave, $listener, $id) = @_; |
195 | my ($slave, $listener, $id) = @_; |
92 | |
196 | |
93 | close $slave; # we do not use the socket, so close it to save resources |
197 | close $slave; # we do not use the socket, so close it to save resources |
94 | |
198 | |
95 | # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
199 | # we could go ballistic and use e.g. AnyEvent here, or IO::AIO, |
… | |
… | |
99 | } |
203 | } |
100 | } |
204 | } |
101 | |
205 | |
102 | =head2 use AnyEvent::Fork as a faster fork+exec |
206 | =head2 use AnyEvent::Fork as a faster fork+exec |
103 | |
207 | |
104 | This runs /bin/echo hi, with stdout redirected to /tmp/log and stderr to |
208 | This runs C</bin/echo hi>, with stdandard output redirected to /tmp/log |
105 | the communications socket. It is usually faster than fork+exec, but still |
209 | and standard error redirected to the communications socket. It is usually |
106 | let's you prepare the environment. |
210 | faster than fork+exec, but still lets you prepare the environment. |
107 | |
211 | |
108 | open my $output, ">/tmp/log" or die "$!"; |
212 | open my $output, ">/tmp/log" or die "$!"; |
109 | |
213 | |
110 | AnyEvent::Fork |
214 | AnyEvent::Fork |
111 | ->new |
215 | ->new |
… | |
… | |
123 | ->send_fh ($output) |
227 | ->send_fh ($output) |
124 | ->send_arg ("/bin/echo", "hi") |
228 | ->send_arg ("/bin/echo", "hi") |
125 | ->run ("run", my $cv = AE::cv); |
229 | ->run ("run", my $cv = AE::cv); |
126 | |
230 | |
127 | my $stderr = $cv->recv; |
231 | my $stderr = $cv->recv; |
128 | |
|
|
129 | =head1 PROBLEM STATEMENT |
|
|
130 | |
|
|
131 | There are two ways to implement parallel processing on UNIX like operating |
|
|
132 | systems - fork and process, and fork+exec and process. They have different |
|
|
133 | advantages and disadvantages that I describe below, together with how this |
|
|
134 | module tries to mitigate the disadvantages. |
|
|
135 | |
|
|
136 | =over 4 |
|
|
137 | |
|
|
138 | =item Forking from a big process can be very slow (a 5GB process needs |
|
|
139 | 0.05s to fork on my 3.6GHz amd64 GNU/Linux box for example). This overhead |
|
|
140 | is often shared with exec (because you have to fork first), but in some |
|
|
141 | circumstances (e.g. when vfork is used), fork+exec can be much faster. |
|
|
142 | |
|
|
143 | This module can help here by telling a small(er) helper process to fork, |
|
|
144 | or fork+exec instead. |
|
|
145 | |
|
|
146 | =item Forking usually creates a copy-on-write copy of the parent |
|
|
147 | process. Memory (for example, modules or data files that have been |
|
|
148 | will not take additional memory). When exec'ing a new process, modules |
|
|
149 | and data files might need to be loaded again, at extra CPU and memory |
|
|
150 | cost. Likewise when forking, all data structures are copied as well - if |
|
|
151 | the program frees them and replaces them by new data, the child processes |
|
|
152 | will retain the memory even if it isn't used. |
|
|
153 | |
|
|
154 | This module allows the main program to do a controlled fork, and allows |
|
|
155 | modules to exec processes safely at any time. When creating a custom |
|
|
156 | process pool you can take advantage of data sharing via fork without |
|
|
157 | risking to share large dynamic data structures that will blow up child |
|
|
158 | memory usage. |
|
|
159 | |
|
|
160 | =item Exec'ing a new perl process might be difficult and slow. For |
|
|
161 | example, it is not easy to find the correct path to the perl interpreter, |
|
|
162 | and all modules have to be loaded from disk again. Long running processes |
|
|
163 | might run into problems when perl is upgraded for example. |
|
|
164 | |
|
|
165 | This module supports creating pre-initialised perl processes to be used |
|
|
166 | as template, and also tries hard to identify the correct path to the perl |
|
|
167 | interpreter. With a cooperative main program, exec'ing the interpreter |
|
|
168 | might not even be necessary. |
|
|
169 | |
|
|
170 | =item Forking might be impossible when a program is running. For example, |
|
|
171 | POSIX makes it almost impossible to fork from a multi-threaded program and |
|
|
172 | do anything useful in the child - strictly speaking, if your perl program |
|
|
173 | uses posix threads (even indirectly via e.g. L<IO::AIO> or L<threads>), |
|
|
174 | you cannot call fork on the perl level anymore, at all. |
|
|
175 | |
|
|
176 | This module can safely fork helper processes at any time, by calling |
|
|
177 | fork+exec in C, in a POSIX-compatible way. |
|
|
178 | |
|
|
179 | =item Parallel processing with fork might be inconvenient or difficult |
|
|
180 | to implement. For example, when a program uses an event loop and creates |
|
|
181 | watchers it becomes very hard to use the event loop from a child |
|
|
182 | program, as the watchers already exist but are only meaningful in the |
|
|
183 | parent. Worse, a module might want to use such a system, not knowing |
|
|
184 | whether another module or the main program also does, leading to problems. |
|
|
185 | |
|
|
186 | This module only lets the main program create pools by forking (because |
|
|
187 | only the main program can know when it is still safe to do so) - all other |
|
|
188 | pools are created by fork+exec, after which such modules can again be |
|
|
189 | loaded. |
|
|
190 | |
|
|
191 | =back |
|
|
192 | |
232 | |
193 | =head1 CONCEPTS |
233 | =head1 CONCEPTS |
194 | |
234 | |
195 | This module can create new processes either by executing a new perl |
235 | This module can create new processes either by executing a new perl |
196 | process, or by forking from an existing "template" process. |
236 | process, or by forking from an existing "template" process. |
… | |
… | |
275 | my ($fork_fh) = @_; |
315 | my ($fork_fh) = @_; |
276 | }); |
316 | }); |
277 | |
317 | |
278 | =back |
318 | =back |
279 | |
319 | |
280 | =head1 FUNCTIONS |
320 | =head1 THE C<AnyEvent::Fork> CLASS |
|
|
321 | |
|
|
322 | This module exports nothing, and only implements a single class - |
|
|
323 | C<AnyEvent::Fork>. |
|
|
324 | |
|
|
325 | There are two class constructors that both create new processes - C<new> |
|
|
326 | and C<new_exec>. The C<fork> method creates a new process by forking an |
|
|
327 | existing one and could be considered a third constructor. |
|
|
328 | |
|
|
329 | Most of the remaining methods deal with preparing the new process, by |
|
|
330 | loading code, evaluating code and sending data to the new process. They |
|
|
331 | usually return the process object, so you can chain method calls. |
|
|
332 | |
|
|
333 | If a process object is destroyed before calling its C<run> method, then |
|
|
334 | the process simply exits. After C<run> is called, all responsibility is |
|
|
335 | passed to the specified function. |
|
|
336 | |
|
|
337 | As long as there is any outstanding work to be done, process objects |
|
|
338 | resist being destroyed, so there is no reason to store them unless you |
|
|
339 | need them later - configure and forget works just fine. |
281 | |
340 | |
282 | =over 4 |
341 | =over 4 |
283 | |
342 | |
284 | =cut |
343 | =cut |
285 | |
344 | |
… | |
… | |
295 | use IO::FDPass; |
354 | use IO::FDPass; |
296 | |
355 | |
297 | our $VERSION = 0.5; |
356 | our $VERSION = 0.5; |
298 | |
357 | |
299 | our $PERL; # the path to the perl interpreter, deduces with various forms of magic |
358 | our $PERL; # the path to the perl interpreter, deduces with various forms of magic |
300 | |
|
|
301 | =item my $pool = new AnyEvent::Fork key => value... |
|
|
302 | |
|
|
303 | Create a new process pool. The following named parameters are supported: |
|
|
304 | |
359 | |
305 | =over 4 |
360 | =over 4 |
306 | |
361 | |
307 | =back |
362 | =back |
308 | |
363 | |
… | |
… | |
404 | Create a new "empty" perl interpreter process and returns its process |
459 | Create a new "empty" perl interpreter process and returns its process |
405 | object for further manipulation. |
460 | object for further manipulation. |
406 | |
461 | |
407 | The new process is forked from a template process that is kept around |
462 | The new process is forked from a template process that is kept around |
408 | for this purpose. When it doesn't exist yet, it is created by a call to |
463 | for this purpose. When it doesn't exist yet, it is created by a call to |
409 | C<new_exec> and kept around for future calls. |
464 | C<new_exec> first and then stays around for future calls. |
410 | |
|
|
411 | When the process object is destroyed, it will release the file handle |
|
|
412 | that connects it with the new process. When the new process has not yet |
|
|
413 | called C<run>, then the process will exit. Otherwise, what happens depends |
|
|
414 | entirely on the code that is executed. |
|
|
415 | |
465 | |
416 | =cut |
466 | =cut |
417 | |
467 | |
418 | sub new { |
468 | sub new { |
419 | my $class = shift; |
469 | my $class = shift; |
… | |
… | |
509 | } |
559 | } |
510 | |
560 | |
511 | =item $pid = $proc->pid |
561 | =item $pid = $proc->pid |
512 | |
562 | |
513 | Returns the process id of the process I<iff it is a direct child of the |
563 | Returns the process id of the process I<iff it is a direct child of the |
514 | process> running AnyEvent::Fork, and C<undef> otherwise. |
564 | process running AnyEvent::Fork>, and C<undef> otherwise. |
515 | |
565 | |
516 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
566 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
517 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
567 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
518 | to clean up their zombies when they die. |
568 | to clean up their zombies when they die. |
519 | |
569 | |
520 | All other processes are not direct children, and will be cleaned up by |
570 | All other processes are not direct children, and will be cleaned up by |
521 | AnyEvent::Fork. |
571 | AnyEvent::Fork itself. |
522 | |
572 | |
523 | =cut |
573 | =cut |
524 | |
574 | |
525 | sub pid { |
575 | sub pid { |
526 | $_[0][0] |
576 | $_[0][0] |
… | |
… | |
537 | |
587 | |
538 | The code will usually be executed after this call returns, and there is no |
588 | The code will usually be executed after this call returns, and there is no |
539 | way to pass anything back to the calling process. Any evaluation errors |
589 | way to pass anything back to the calling process. Any evaluation errors |
540 | will be reported to stderr and cause the process to exit. |
590 | will be reported to stderr and cause the process to exit. |
541 | |
591 | |
542 | If you want to execute some code to take over the process (see the |
592 | If you want to execute some code (that isn't in a module) to take over the |
543 | "fork+exec" example in the SYNOPSIS), you should compile a function via |
593 | process, you should compile a function via C<eval> first, and then call |
544 | C<eval> first, and then call it via C<run>. This also gives you access to |
594 | it via C<run>. This also gives you access to any arguments passed via the |
545 | any arguments passed via the C<send_xxx> methods, such as file handles. |
595 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
|
|
596 | a faster fork+exec> example to see it in action. |
546 | |
597 | |
547 | Returns the process object for easy chaining of method calls. |
598 | Returns the process object for easy chaining of method calls. |
548 | |
599 | |
549 | =cut |
600 | =cut |
550 | |
601 | |
… | |
… | |
576 | =item $proc = $proc->send_fh ($handle, ...) |
627 | =item $proc = $proc->send_fh ($handle, ...) |
577 | |
628 | |
578 | Send one or more file handles (I<not> file descriptors) to the process, |
629 | Send one or more file handles (I<not> file descriptors) to the process, |
579 | to prepare a call to C<run>. |
630 | to prepare a call to C<run>. |
580 | |
631 | |
581 | The process object keeps a reference to the handles until this is done, |
632 | The process object keeps a reference to the handles until they have |
582 | so you must not explicitly close the handles. This is most easily |
633 | been passed over to the process, so you must not explicitly close the |
583 | accomplished by simply not storing the file handles anywhere after passing |
634 | handles. This is most easily accomplished by simply not storing the file |
584 | them to this method. |
635 | handles anywhere after passing them to this method - when AnyEvent::Fork |
|
|
636 | is finished using them, perl will automatically close them. |
585 | |
637 | |
586 | Returns the process object for easy chaining of method calls. |
638 | Returns the process object for easy chaining of method calls. |
587 | |
639 | |
588 | Example: pass a file handle to a process, and release it without |
640 | Example: pass a file handle to a process, and release it without |
589 | closing. It will be closed automatically when it is no longer used. |
641 | closing. It will be closed automatically when it is no longer used. |
… | |
… | |
605 | } |
657 | } |
606 | |
658 | |
607 | =item $proc = $proc->send_arg ($string, ...) |
659 | =item $proc = $proc->send_arg ($string, ...) |
608 | |
660 | |
609 | Send one or more argument strings to the process, to prepare a call to |
661 | Send one or more argument strings to the process, to prepare a call to |
610 | C<run>. The strings can be any octet string. |
662 | C<run>. The strings can be any octet strings. |
611 | |
663 | |
612 | The protocol is optimised to pass a moderate number of relatively short |
664 | The protocol is optimised to pass a moderate number of relatively short |
613 | strings - while you can pass up to 4GB of data in one go, this is more |
665 | strings - while you can pass up to 4GB of data in one go, this is more |
614 | meant to pass some ID information or other startup info, not big chunks of |
666 | meant to pass some ID information or other startup info, not big chunks of |
615 | data. |
667 | data. |
… | |
… | |
631 | Enter the function specified by the function name in C<$func> in the |
683 | Enter the function specified by the function name in C<$func> in the |
632 | process. The function is called with the communication socket as first |
684 | process. The function is called with the communication socket as first |
633 | argument, followed by all file handles and string arguments sent earlier |
685 | argument, followed by all file handles and string arguments sent earlier |
634 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
686 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
635 | |
687 | |
|
|
688 | The process object becomes unusable on return from this function - any |
|
|
689 | further method calls result in undefined behaviour. |
|
|
690 | |
636 | The function name should be fully qualified, but if it isn't, it will be |
691 | The function name should be fully qualified, but if it isn't, it will be |
637 | looked up in the main package. |
692 | looked up in the C<main> package. |
638 | |
693 | |
639 | If the called function returns, doesn't exist, or any error occurs, the |
694 | If the called function returns, doesn't exist, or any error occurs, the |
640 | process exits. |
695 | process exits. |
641 | |
696 | |
642 | Preparing the process is done in the background - when all commands have |
697 | Preparing the process is done in the background - when all commands have |
643 | been sent, the callback is invoked with the local communications socket |
698 | been sent, the callback is invoked with the local communications socket |
644 | as argument. At this point you can start using the socket in any way you |
699 | as argument. At this point you can start using the socket in any way you |
645 | like. |
700 | like. |
646 | |
|
|
647 | The process object becomes unusable on return from this function - any |
|
|
648 | further method calls result in undefined behaviour. |
|
|
649 | |
701 | |
650 | If the communication socket isn't used, it should be closed on both sides, |
702 | If the communication socket isn't used, it should be closed on both sides, |
651 | to save on kernel memory. |
703 | to save on kernel memory. |
652 | |
704 | |
653 | The socket is non-blocking in the parent, and blocking in the newly |
705 | The socket is non-blocking in the parent, and blocking in the newly |
… | |
… | |
728 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
780 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
729 | |
781 | |
730 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
782 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
731 | though it uses the same operations, but adds a lot of overhead? |
783 | though it uses the same operations, but adds a lot of overhead? |
732 | |
784 | |
733 | The difference is simply the process size: forking the 6MB process takes |
785 | The difference is simply the process size: forking the 5MB process takes |
734 | so much longer than forking the 2.5MB template process that the overhead |
786 | so much longer than forking the 2.5MB template process that the extra |
735 | introduced is canceled out. |
787 | overhead introduced is canceled out. |
736 | |
788 | |
737 | If the benchmark process grows, the normal fork becomes even slower: |
789 | If the benchmark process grows, the normal fork becomes even slower: |
738 | |
790 | |
739 | 1340 new processes, manual fork in a 20MB process |
791 | 1340 new processes, manual fork of a 20MB process |
740 | 731 new processes, manual fork in a 200MB process |
792 | 731 new processes, manual fork of a 200MB process |
741 | 235 new processes, manual fork in a 2000MB process |
793 | 235 new processes, manual fork of a 2000MB process |
742 | |
794 | |
743 | What that means (to me) is that I can use this module without having a |
795 | What that means (to me) is that I can use this module without having a bad |
744 | very bad conscience because of the extra overhead required to start new |
796 | conscience because of the extra overhead required to start new processes. |
745 | processes. |
|
|
746 | |
797 | |
747 | =head1 TYPICAL PROBLEMS |
798 | =head1 TYPICAL PROBLEMS |
748 | |
799 | |
749 | This section lists typical problems that remain. I hope by recognising |
800 | This section lists typical problems that remain. I hope by recognising |
750 | them, most can be avoided. |
801 | them, most can be avoided. |
751 | |
802 | |
752 | =over 4 |
803 | =over 4 |
753 | |
804 | |
754 | =item "leaked" file descriptors for exec'ed processes |
805 | =item leaked file descriptors for exec'ed processes |
755 | |
806 | |
756 | POSIX systems inherit file descriptors by default when exec'ing a new |
807 | POSIX systems inherit file descriptors by default when exec'ing a new |
757 | process. While perl itself laudably sets the close-on-exec flags on new |
808 | process. While perl itself laudably sets the close-on-exec flags on new |
758 | file handles, most C libraries don't care, and even if all cared, it's |
809 | file handles, most C libraries don't care, and even if all cared, it's |
759 | often not possible to set the flag in a race-free manner. |
810 | often not possible to set the flag in a race-free manner. |
… | |
… | |
779 | libraries or the code that leaks those file descriptors. |
830 | libraries or the code that leaks those file descriptors. |
780 | |
831 | |
781 | Fortunately, most of these leaked descriptors do no harm, other than |
832 | Fortunately, most of these leaked descriptors do no harm, other than |
782 | sitting on some resources. |
833 | sitting on some resources. |
783 | |
834 | |
784 | =item "leaked" file descriptors for fork'ed processes |
835 | =item leaked file descriptors for fork'ed processes |
785 | |
836 | |
786 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
837 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
787 | which closes file descriptors not marked for being inherited. |
838 | which closes file descriptors not marked for being inherited. |
788 | |
839 | |
789 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
840 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
… | |
… | |
798 | |
849 | |
799 | The solution is to either not load these modules before use'ing |
850 | The solution is to either not load these modules before use'ing |
800 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay |
851 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay |
801 | initialising them, for example, by calling C<init Gtk2> manually. |
852 | initialising them, for example, by calling C<init Gtk2> manually. |
802 | |
853 | |
803 | =item exit runs destructors |
854 | =item exiting calls object destructors |
804 | |
855 | |
805 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
856 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
806 | L<AnyEvent::Fork::Template>. |
857 | L<AnyEvent::Fork::Template>. |
807 | |
858 | |
808 | When a process created by AnyEvent::Fork exits, it might do so by calling |
859 | When a process created by AnyEvent::Fork exits, it might do so by calling |
… | |
… | |
830 | to make it so, mostly due to the bloody broken perl that nobody seems to |
881 | to make it so, mostly due to the bloody broken perl that nobody seems to |
831 | care about. The fork emulation is a bad joke - I have yet to see something |
882 | care about. The fork emulation is a bad joke - I have yet to see something |
832 | useful that you can do with it without running into memory corruption |
883 | useful that you can do with it without running into memory corruption |
833 | issues or other braindamage. Hrrrr. |
884 | issues or other braindamage. Hrrrr. |
834 | |
885 | |
835 | Cygwin perl is not supported at the moment, as it should implement fd |
886 | Cygwin perl is not supported at the moment due to some hilarious |
836 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
887 | shortcomings of its API - see L<IO::FDPoll> for more details. |
837 | support enough functionality to do it. |
|
|
838 | |
888 | |
839 | =head1 SEE ALSO |
889 | =head1 SEE ALSO |
840 | |
890 | |
841 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
891 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
842 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
892 | L<AnyEvent::Fork::Template> (to create a process by forking the main |