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

Comparing AnyEvent-Fork/Fork.pm (file contents):
Revision 1.24 by root, Sat Apr 6 08:32:23 2013 UTC vs.
Revision 1.25 by root, Sat Apr 6 08:55:16 2013 UTC

39If you need some form of RPC, you can either implement it yourself 39If you need some form of RPC, you can either implement it yourself
40in whatever way you like, use some message-passing module such 40in whatever way you like, use some message-passing module such
41as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use 41as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use
42L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, 42L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages,
43and so on. 43and so on.
44
45=head1 PROBLEM STATEMENT
46
47There are two traditional ways to implement parallel processing on UNIX
48like operating systems - fork and process, and fork+exec and process. They
49have different advantages and disadvantages that I describe below,
50together 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
56A 5GB process needs 0.05s to fork on my 3.6GHz amd64 GNU/Linux box. This
57overhead is often shared with exec (because you have to fork first), but
58in some circumstances (e.g. when vfork is used), fork+exec can be much
59faster.
60
61This module can help here by telling a small(er) helper process to fork,
62which is faster then forking the main process, and also uses vfork where
63possible. 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
66process.
67
68For example, modules or data files that are loaded will not use additional
69memory after a fork. When exec'ing a new process, modules and data files
70might need to be loaded again, at extra CPU and memory cost. But when
71forking, literally all data structures are copied - if the program frees
72them and replaces them by new data, the child processes will retain the
73old version even if it isn't used, which can suddenly and unexpectedly
74increase memory usage when freeing memory.
75
76The trade-off is between more sharing with fork (which can be good or
77bad), and no sharing with exec.
78
79This module allows the main program to do a controlled fork, and allows
80modules to exec processes safely at any time. When creating a custom
81process pool you can take advantage of data sharing via fork without
82risking to share large dynamic data structures that will blow up child
83memory usage.
84
85In other words, this module puts you into control over what is being
86shared and what isn't, at all times.
87
88=item Exec'ing a new perl process might be difficult.
89
90For example, it is not easy to find the correct path to the perl
91interpreter - C<$^X> might not be a perl interpreter at all.
92
93This module tries hard to identify the correct path to the perl
94interpreter. With a cooperative main program, exec'ing the interpreter
95might not even be necessary, but even without help from the main program,
96it will still work when used from a module.
97
98=item Exec'ing a new perl process might be slow, as all necessary modules
99have to be loaded from disk again, with no guarantees of success.
100
101Long running processes might run into problems when perl is upgraded
102and modules are no longer loadable because they refer to a different
103perl version, or parts of a distribution are newer than the ones already
104loaded.
105
106This module supports creating pre-initialised perl processes to be used as
107a template for new processes.
108
109=item Forking might be impossible when a program is running.
110
111For example, POSIX makes it almost impossible to fork from a
112multi-threaded program while doing anything useful in the child - in
113fact, if your perl program uses POSIX threads (even indirectly via
114e.g. L<IO::AIO> or L<threads>), you cannot call fork on the perl level
115anymore without risking corruption issues on a number of operating
116systems.
117
118This module can safely fork helper processes at any time, by calling
119fork+exec in C, in a POSIX-compatible way (via L<Proc::FastSpawn>).
120
121=item Parallel processing with fork might be inconvenient or difficult
122to implement. Modules might not work in both parent and child.
123
124For example, when a program uses an event loop and creates watchers it
125becomes very hard to use the event loop from a child program, as the
126watchers already exist but are only meaningful in the parent. Worse, a
127module might want to use such a module, not knowing whether another module
128or the main program also does, leading to problems.
129
130With this module only the main program is allowed to create new processes
131by forking (because only the main program can know when it is still safe
132to do so) - all other processes are created via fork+exec, which makes it
133possible to use modules such as event loops or window interfaces safely.
134
135=back
44 136
45=head1 EXAMPLES 137=head1 EXAMPLES
46 138
47=head2 Create a single new process, tell it to run your worker function. 139=head2 Create a single new process, tell it to run your worker function.
48 140
123 ->send_fh ($output) 215 ->send_fh ($output)
124 ->send_arg ("/bin/echo", "hi") 216 ->send_arg ("/bin/echo", "hi")
125 ->run ("run", my $cv = AE::cv); 217 ->run ("run", my $cv = AE::cv);
126 218
127 my $stderr = $cv->recv; 219 my $stderr = $cv->recv;
128
129=head1 PROBLEM STATEMENT
130
131There are two ways to implement parallel processing on UNIX like operating
132systems - fork and process, and fork+exec and process. They have different
133advantages and disadvantages that I describe below, together with how this
134module 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
1390.05s to fork on my 3.6GHz amd64 GNU/Linux box for example). This overhead
140is often shared with exec (because you have to fork first), but in some
141circumstances (e.g. when vfork is used), fork+exec can be much faster.
142
143This module can help here by telling a small(er) helper process to fork,
144or fork+exec instead.
145
146=item Forking usually creates a copy-on-write copy of the parent
147process. Memory (for example, modules or data files that have been
148will not take additional memory). When exec'ing a new process, modules
149and data files might need to be loaded again, at extra CPU and memory
150cost. Likewise when forking, all data structures are copied as well - if
151the program frees them and replaces them by new data, the child processes
152will retain the memory even if it isn't used.
153
154This module allows the main program to do a controlled fork, and allows
155modules to exec processes safely at any time. When creating a custom
156process pool you can take advantage of data sharing via fork without
157risking to share large dynamic data structures that will blow up child
158memory usage.
159
160=item Exec'ing a new perl process might be difficult and slow. For
161example, it is not easy to find the correct path to the perl interpreter,
162and all modules have to be loaded from disk again. Long running processes
163might run into problems when perl is upgraded for example.
164
165This module supports creating pre-initialised perl processes to be used
166as template, and also tries hard to identify the correct path to the perl
167interpreter. With a cooperative main program, exec'ing the interpreter
168might not even be necessary.
169
170=item Forking might be impossible when a program is running. For example,
171POSIX makes it almost impossible to fork from a multi-threaded program and
172do anything useful in the child - strictly speaking, if your perl program
173uses posix threads (even indirectly via e.g. L<IO::AIO> or L<threads>),
174you cannot call fork on the perl level anymore, at all.
175
176This module can safely fork helper processes at any time, by calling
177fork+exec in C, in a POSIX-compatible way.
178
179=item Parallel processing with fork might be inconvenient or difficult
180to implement. For example, when a program uses an event loop and creates
181watchers it becomes very hard to use the event loop from a child
182program, as the watchers already exist but are only meaningful in the
183parent. Worse, a module might want to use such a system, not knowing
184whether another module or the main program also does, leading to problems.
185
186This module only lets the main program create pools by forking (because
187only the main program can know when it is still safe to do so) - all other
188pools are created by fork+exec, after which such modules can again be
189loaded.
190
191=back
192 220
193=head1 CONCEPTS 221=head1 CONCEPTS
194 222
195This module can create new processes either by executing a new perl 223This module can create new processes either by executing a new perl
196process, or by forking from an existing "template" process. 224process, or by forking from an existing "template" process.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines