… | |
… | |
2 | |
2 | |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::Fork; |
|
|
8 | use AnyEvent::Fork::RPC; |
7 | use AnyEvent::Fork::RPC; |
|
|
8 | # use AnyEvent::Fork is not needed |
9 | |
9 | |
10 | my $rpc = AnyEvent::Fork |
10 | my $rpc = AnyEvent::Fork |
11 | ->new |
11 | ->new |
12 | ->require ("MyModule") |
12 | ->require ("MyModule") |
13 | ->AnyEvent::Fork::RPC::run ( |
13 | ->AnyEvent::Fork::RPC::run ( |
… | |
… | |
34 | concurrently in the child, using AnyEvent. |
34 | concurrently in the child, using AnyEvent. |
35 | |
35 | |
36 | It also implements an asynchronous event mechanism from the child to the |
36 | It also implements an asynchronous event mechanism from the child to the |
37 | parent, that could be used for progress indications or other information. |
37 | parent, that could be used for progress indications or other information. |
38 | |
38 | |
|
|
39 | Loading this module also always loads L<AnyEvent::Fork>, so you can make a |
|
|
40 | separate C<use AnyEvent::Fork> if you wish, but you don't have to. |
|
|
41 | |
39 | =head1 EXAMPLES |
42 | =head1 EXAMPLES |
40 | |
43 | |
41 | =head2 Synchronous Backend |
44 | =head2 Example 1: Synchronous Backend |
42 | |
45 | |
43 | Here is a simple example that implements a backend that executes C<unlink> |
46 | Here is a simple example that implements a backend that executes C<unlink> |
44 | and C<rmdir> calls, and reports their status back. It also reports the |
47 | and C<rmdir> calls, and reports their status back. It also reports the |
45 | number of requests it has processed every three requests, which is clearly |
48 | number of requests it has processed every three requests, which is clearly |
46 | silly, but illustrates the use of events. |
49 | silly, but illustrates the use of events. |
47 | |
50 | |
48 | First the parent process: |
51 | First the parent process: |
49 | |
52 | |
50 | use AnyEvent; |
53 | use AnyEvent; |
51 | use AnyEvent::Fork; |
|
|
52 | use AnyEvent::Fork::RPC; |
54 | use AnyEvent::Fork::RPC; |
53 | |
55 | |
54 | my $done = AE::cv; |
56 | my $done = AE::cv; |
55 | |
57 | |
56 | my $rpc = AnyEvent::Fork |
58 | my $rpc = AnyEvent::Fork |
57 | ->new |
59 | ->new |
58 | ->require ("MyWorker") |
60 | ->require ("MyWorker") |
59 | ->AnyEvent::Fork::RPC::run ("MyWorker::run", |
61 | ->AnyEvent::Fork::RPC::run ("MyWorker::run", |
|
|
62 | on_error => sub { warn "FATAL: $_[0]"; exit 1 }, |
60 | on_event => sub { warn "$_[0] requests handled\n" }, |
63 | on_event => sub { warn "$_[0] requests handled\n" }, |
61 | on_destroy => $done, |
64 | on_destroy => $done, |
62 | ); |
65 | ); |
63 | |
66 | |
64 | for my $id (1..6) { |
67 | for my $id (1..6) { |
… | |
… | |
102 | dies with a fatal error - obviously, you must never let this happen :). |
105 | dies with a fatal error - obviously, you must never let this happen :). |
103 | |
106 | |
104 | Eventually it returns the status value true if the command was successful, |
107 | Eventually it returns the status value true if the command was successful, |
105 | or the status value 0 and the stringified error message. |
108 | or the status value 0 and the stringified error message. |
106 | |
109 | |
107 | On my system, running the first cdoe fragment with the given |
110 | On my system, running the first code fragment with the given |
108 | F<MyWorker.pm> in the current directory yields: |
111 | F<MyWorker.pm> in the current directory yields: |
109 | |
112 | |
110 | /tmp/somepath/1: No such file or directory |
113 | /tmp/somepath/1: No such file or directory |
111 | /tmp/somepath/2: No such file or directory |
114 | /tmp/somepath/2: No such file or directory |
112 | 3 requests handled |
115 | 3 requests handled |
… | |
… | |
133 | |
136 | |
134 | And as a final remark, there is a fine module on CPAN that can |
137 | And as a final remark, there is a fine module on CPAN that can |
135 | asynchronously C<rmdir> and C<unlink> and a lot more, and more efficiently |
138 | asynchronously C<rmdir> and C<unlink> and a lot more, and more efficiently |
136 | than this example, namely L<IO::AIO>. |
139 | than this example, namely L<IO::AIO>. |
137 | |
140 | |
|
|
141 | =head3 Example 1a: the same with the asynchronous backend |
|
|
142 | |
|
|
143 | This example only shows what needs to be changed to use the async backend |
|
|
144 | instead. Doing this is not very useful, the purpose of this example is |
|
|
145 | to show the minimum amount of change that is required to go from the |
|
|
146 | synchronous to the asynchronous backend. |
|
|
147 | |
|
|
148 | To use the async backend in the previous example, you need to add the |
|
|
149 | C<async> parameter to the C<AnyEvent::Fork::RPC::run> call: |
|
|
150 | |
|
|
151 | ->AnyEvent::Fork::RPC::run ("MyWorker::run", |
|
|
152 | async => 1, |
|
|
153 | ... |
|
|
154 | |
|
|
155 | And since the function call protocol is now changed, you need to adopt |
|
|
156 | C<MyWorker::run> to the async API. |
|
|
157 | |
|
|
158 | First, you need to accept the extra initial C<$done> callback: |
|
|
159 | |
|
|
160 | sub run { |
|
|
161 | my ($done, $cmd, $path) = @_; |
|
|
162 | |
|
|
163 | And since a response is now generated when C<$done> is called, as opposed |
|
|
164 | to when the function returns, we need to call the C<$done> function with |
|
|
165 | the status: |
|
|
166 | |
|
|
167 | $done->($status or (0, "$!")); |
|
|
168 | |
|
|
169 | A few remarks are in order. First, it's quite pointless to use the async |
|
|
170 | backend for this example - but it I<is> possible. Second, you can call |
|
|
171 | C<$done> before or after returning from the function. Third, having both |
|
|
172 | returned from the function and having called the C<$done> callback, the |
|
|
173 | child process may exit at any time, so you should call C<$done> only when |
|
|
174 | you really I<are> done. |
|
|
175 | |
|
|
176 | =head2 Example 2: Asynchronous Backend |
|
|
177 | |
|
|
178 | This example implements multiple count-downs in the child, using |
|
|
179 | L<AnyEvent> timers. While this is a bit silly (one could use timers in te |
|
|
180 | parent just as well), it illustrates the ability to use AnyEvent in the |
|
|
181 | child and the fact that responses can arrive in a different order then the |
|
|
182 | requests. |
|
|
183 | |
|
|
184 | It also shows how to embed the actual child code into a C<__DATA__> |
|
|
185 | section, so it doesn't need any external files at all. |
|
|
186 | |
|
|
187 | And when your parent process is often busy, and you have stricter timing |
|
|
188 | requirements, then running timers in a child process suddenly doesn't look |
|
|
189 | so silly anymore. |
|
|
190 | |
|
|
191 | Without further ado, here is the code: |
|
|
192 | |
|
|
193 | use AnyEvent; |
|
|
194 | use AnyEvent::Fork::RPC; |
|
|
195 | |
|
|
196 | my $done = AE::cv; |
|
|
197 | |
|
|
198 | my $rpc = AnyEvent::Fork |
|
|
199 | ->new |
|
|
200 | ->require ("AnyEvent::Fork::RPC::Async") |
|
|
201 | ->eval (do { local $/; <DATA> }) |
|
|
202 | ->AnyEvent::Fork::RPC::run ("run", |
|
|
203 | async => 1, |
|
|
204 | on_error => sub { warn "FATAL: $_[0]"; exit 1 }, |
|
|
205 | on_event => sub { print $_[0] }, |
|
|
206 | on_destroy => $done, |
|
|
207 | ); |
|
|
208 | |
|
|
209 | for my $count (3, 2, 1) { |
|
|
210 | $rpc->($count, sub { |
|
|
211 | warn "job $count finished\n"; |
|
|
212 | }); |
|
|
213 | } |
|
|
214 | |
|
|
215 | undef $rpc; |
|
|
216 | |
|
|
217 | $done->recv; |
|
|
218 | |
|
|
219 | __DATA__ |
|
|
220 | |
|
|
221 | # this ends up in main, as we don't use a package declaration |
|
|
222 | |
|
|
223 | use AnyEvent; |
|
|
224 | |
|
|
225 | sub run { |
|
|
226 | my ($done, $count) = @_; |
|
|
227 | |
|
|
228 | my $n; |
|
|
229 | |
|
|
230 | AnyEvent::Fork::RPC::event "starting to count up to $count\n"; |
|
|
231 | |
|
|
232 | my $w; $w = AE::timer 1, 1, sub { |
|
|
233 | ++$n; |
|
|
234 | |
|
|
235 | AnyEvent::Fork::RPC::event "count $n of $count\n"; |
|
|
236 | |
|
|
237 | if ($n == $count) { |
|
|
238 | undef $w; |
|
|
239 | $done->(); |
|
|
240 | } |
|
|
241 | }; |
|
|
242 | } |
|
|
243 | |
|
|
244 | The parent part (the one before the C<__DATA__> section) isn't very |
|
|
245 | different from the earlier examples. It sets async mode, preloads |
|
|
246 | the backend module (so the C<AnyEvent::Fork::RPC::event> function is |
|
|
247 | declared), uses a slightly different C<on_event> handler (which we use |
|
|
248 | simply for logging purposes) and then, instead of loading a module with |
|
|
249 | the actual worker code, it C<eval>'s the code from the data section in the |
|
|
250 | child process. |
|
|
251 | |
|
|
252 | It then starts three countdowns, from 3 to 1 seconds downwards, destroys |
|
|
253 | the rpc object so the example finishes eventually, and then just waits for |
|
|
254 | the stuff to trickle in. |
|
|
255 | |
|
|
256 | The worker code uses the event function to log some progress messages, but |
|
|
257 | mostly just creates a recurring one-second timer. |
|
|
258 | |
|
|
259 | The timer callback increments a counter, logs a message, and eventually, |
|
|
260 | when the count has been reached, calls the finish callback. |
|
|
261 | |
|
|
262 | On my system, this results in the following output. Since all timers fire |
|
|
263 | at roughly the same time, the actual order isn't guaranteed, but the order |
|
|
264 | shown is very likely what you would get, too. |
|
|
265 | |
|
|
266 | starting to count up to 3 |
|
|
267 | starting to count up to 2 |
|
|
268 | starting to count up to 1 |
|
|
269 | count 1 of 3 |
|
|
270 | count 1 of 2 |
|
|
271 | count 1 of 1 |
|
|
272 | job 1 finished |
|
|
273 | count 2 of 2 |
|
|
274 | job 2 finished |
|
|
275 | count 2 of 3 |
|
|
276 | count 3 of 3 |
|
|
277 | job 3 finished |
|
|
278 | |
|
|
279 | While the overall ordering isn't guaranteed, the async backend still |
|
|
280 | guarantees that events and responses are delivered to the parent process |
|
|
281 | in the exact same ordering as they were generated in the child process. |
|
|
282 | |
|
|
283 | And unless your system is I<very> busy, it should clearly show that the |
|
|
284 | job started last will finish first, as it has the lowest count. |
|
|
285 | |
|
|
286 | This concludes the async example. Since L<AnyEvent::Fork> does not |
|
|
287 | actually fork, you are free to use about any module in the child, not just |
|
|
288 | L<AnyEvent>, but also L<IO::AIO>, or L<Tk> for example. |
|
|
289 | |
138 | =head1 PARENT PROCESS USAGE |
290 | =head1 PARENT PROCESS USAGE |
139 | |
291 | |
140 | This module exports nothing, and only implements a single function: |
292 | This module exports nothing, and only implements a single function: |
141 | |
293 | |
142 | =over 4 |
294 | =over 4 |
… | |
… | |
149 | |
301 | |
150 | use Errno (); |
302 | use Errno (); |
151 | use Guard (); |
303 | use Guard (); |
152 | |
304 | |
153 | use AnyEvent; |
305 | use AnyEvent; |
154 | #use AnyEvent::Fork; |
306 | use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
155 | |
307 | |
156 | our $VERSION = 0.1; |
308 | our $VERSION = 0.1; |
157 | |
309 | |
158 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
310 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
159 | |
311 | |
… | |
… | |
197 | |
349 | |
198 | Called when the C<$rpc> object has been destroyed and all requests have |
350 | Called when the C<$rpc> object has been destroyed and all requests have |
199 | been successfully handled. This is useful when you queue some requests and |
351 | been successfully handled. This is useful when you queue some requests and |
200 | want the child to go away after it has handled them. The problem is that |
352 | want the child to go away after it has handled them. The problem is that |
201 | the parent must not exit either until all requests have been handled, and |
353 | the parent must not exit either until all requests have been handled, and |
202 | this cna be accomplished by waiting for this callback. |
354 | this can be accomplished by waiting for this callback. |
203 | |
355 | |
204 | =item init => $function (default none) |
356 | =item init => $function (default none) |
205 | |
357 | |
206 | When specified (by name), this function is called in the child as the very |
358 | When specified (by name), this function is called in the child as the very |
207 | first thing when taking over the process, with all the arguments normally |
359 | first thing when taking over the process, with all the arguments normally |
… | |
… | |
230 | If you want to pre-load the actual back-end modules to enable memory |
382 | If you want to pre-load the actual back-end modules to enable memory |
231 | sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for |
383 | sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for |
232 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
384 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
233 | |
385 | |
234 | If you use a template process and want to fork both sync and async |
386 | If you use a template process and want to fork both sync and async |
235 | children, then it is permissible to laod both modules. |
387 | children, then it is permissible to load both modules. |
236 | |
388 | |
237 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
389 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
238 | |
390 | |
239 | All arguments, result data and event data have to be serialised to be |
391 | All arguments, result data and event data have to be serialised to be |
240 | transferred between the processes. For this, they have to be frozen and |
392 | transferred between the processes. For this, they have to be frozen and |
… | |
… | |
252 | If you need an external module for serialisation, then you can either |
404 | If you need an external module for serialisation, then you can either |
253 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
405 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
254 | or C<require> statement into the serialiser string. Or both. |
406 | or C<require> statement into the serialiser string. Or both. |
255 | |
407 | |
256 | =back |
408 | =back |
|
|
409 | |
|
|
410 | See the examples section earlier in this document for some actual |
|
|
411 | examples. |
257 | |
412 | |
258 | =cut |
413 | =cut |
259 | |
414 | |
260 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
415 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
261 | |
416 | |
… | |
… | |
275 | # default for on_event is to raise an error |
430 | # default for on_event is to raise an error |
276 | $on_event ||= sub { $on_error->("event received, but no on_event handler") }; |
431 | $on_event ||= sub { $on_error->("event received, but no on_event handler") }; |
277 | |
432 | |
278 | my ($f, $t) = eval $serialiser; die $@ if $@; |
433 | my ($f, $t) = eval $serialiser; die $@ if $@; |
279 | |
434 | |
280 | my (@rcb, $fh, $shutdown, $wbuf, $ww, $rw); |
435 | my (@rcb, %rcb, $fh, $shutdown, $wbuf, $ww); |
281 | my ($rlen, $rbuf) = 512 - 16; |
436 | my ($rlen, $rbuf, $rw) = 512 - 16; |
282 | |
437 | |
283 | my $wcb = sub { |
438 | my $wcb = sub { |
284 | my $len = syswrite $fh, $wbuf; |
439 | my $len = syswrite $fh, $wbuf; |
285 | |
440 | |
286 | if (!defined $len) { |
441 | unless (defined $len) { |
287 | if ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
442 | if ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
288 | undef $rw; undef $ww; # it ends here |
443 | undef $rw; undef $ww; # it ends here |
289 | $on_error->("$!"); |
444 | $on_error->("$!"); |
290 | } |
445 | } |
291 | } |
446 | } |
… | |
… | |
302 | |
457 | |
303 | $self->require ($module) |
458 | $self->require ($module) |
304 | ->send_arg ($function, $arg{init}, $serialiser) |
459 | ->send_arg ($function, $arg{init}, $serialiser) |
305 | ->run ("$module\::run", sub { |
460 | ->run ("$module\::run", sub { |
306 | $fh = shift; |
461 | $fh = shift; |
|
|
462 | |
|
|
463 | my ($id, $len); |
307 | $rw = AE::io $fh, 0, sub { |
464 | $rw = AE::io $fh, 0, sub { |
308 | $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
465 | $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
309 | my $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
466 | $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
310 | |
467 | |
311 | if ($len) { |
468 | if ($len) { |
312 | while (5 <= length $rbuf) { |
469 | while (8 <= length $rbuf) { |
313 | $len = unpack "L", $rbuf; |
470 | ($id, $len) = unpack "LL", $rbuf; |
314 | 4 + $len <= length $rbuf |
471 | 8 + $len <= length $rbuf |
315 | or last; |
472 | or last; |
316 | |
473 | |
317 | my @r = $t->(substr $rbuf, 4, $len); |
474 | my @r = $t->(substr $rbuf, 8, $len); |
318 | substr $rbuf, 0, $len + 4, ""; |
475 | substr $rbuf, 0, 8 + $len, ""; |
|
|
476 | |
|
|
477 | if ($id) { |
|
|
478 | if (@rcb) { |
|
|
479 | (shift @rcb)->(@r); |
|
|
480 | } elsif (my $cb = delete $rcb{$id}) { |
|
|
481 | $cb->(@r); |
|
|
482 | } else { |
|
|
483 | undef $rw; undef $ww; |
|
|
484 | $on_error->("unexpected data from child"); |
319 | |
485 | } |
320 | if (pop @r) { |
486 | } else { |
321 | $on_event->(@r); |
487 | $on_event->(@r); |
322 | } elsif (@rcb) { |
|
|
323 | (shift @rcb)->(@r); |
|
|
324 | } else { |
|
|
325 | undef $rw; undef $ww; |
|
|
326 | $on_error->("unexpected data from child"); |
|
|
327 | } |
488 | } |
328 | } |
489 | } |
329 | } elsif (defined $len) { |
490 | } elsif (defined $len) { |
330 | undef $rw; undef $ww; # it ends here |
491 | undef $rw; undef $ww; # it ends here |
331 | |
492 | |
332 | if (@rcb) { |
493 | if (@rcb || %rcb) { |
333 | $on_error->("unexpected eof"); |
494 | $on_error->("unexpected eof"); |
334 | } else { |
495 | } else { |
335 | $on_destroy->(); |
496 | $on_destroy->(); |
336 | } |
497 | } |
337 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
498 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
… | |
… | |
346 | my $guard = Guard::guard { |
507 | my $guard = Guard::guard { |
347 | $shutdown = 1; |
508 | $shutdown = 1; |
348 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
509 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
349 | }; |
510 | }; |
350 | |
511 | |
|
|
512 | my $id; |
|
|
513 | |
|
|
514 | $arg{async} |
351 | sub { |
515 | ? sub { |
352 | push @rcb, pop; |
516 | $id = ($id == 0xffffffff ? 0 : $id) + 1; |
|
|
517 | $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
353 | |
518 | |
|
|
519 | $rcb{$id} = pop; |
|
|
520 | |
354 | $guard; # keep it alive |
521 | $guard; # keep it alive |
355 | |
522 | |
356 | $wbuf .= pack "L/a*", &$f; |
523 | $wbuf .= pack "LL/a*", $id, &$f; |
357 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
524 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
358 | } |
525 | } |
|
|
526 | : sub { |
|
|
527 | push @rcb, pop; |
|
|
528 | |
|
|
529 | $guard; # keep it alive |
|
|
530 | |
|
|
531 | $wbuf .= pack "L/a*", &$f; |
|
|
532 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
|
|
533 | } |
359 | } |
534 | } |
360 | |
535 | |
361 | =item $rpc->(..., $cb->(...)) |
536 | =item $rpc->(..., $cb->(...)) |
362 | |
537 | |
363 | The RPC object returned by C<AnyEvent::Fork::RPC::run> is actually a code |
538 | The RPC object returned by C<AnyEvent::Fork::RPC::run> is actually a code |
… | |
… | |
378 | |
553 | |
379 | The other thing that can be done with the RPC object is to destroy it. In |
554 | The other thing that can be done with the RPC object is to destroy it. In |
380 | this case, the child process will execute all remaining RPC calls, report |
555 | this case, the child process will execute all remaining RPC calls, report |
381 | their results, and then exit. |
556 | their results, and then exit. |
382 | |
557 | |
|
|
558 | See the examples section earlier in this document for some actual |
|
|
559 | examples. |
|
|
560 | |
383 | =back |
561 | =back |
384 | |
562 | |
385 | =head1 CHILD PROCESS USAGE |
563 | =head1 CHILD PROCESS USAGE |
386 | |
564 | |
387 | The following function is not available in this module. They are only |
565 | The following function is not available in this module. They are only |
… | |
… | |
395 | |
573 | |
396 | Send an event to the parent. Events are a bit like RPC calls made by the |
574 | Send an event to the parent. Events are a bit like RPC calls made by the |
397 | child process to the parent, except that there is no notion of return |
575 | child process to the parent, except that there is no notion of return |
398 | values. |
576 | values. |
399 | |
577 | |
|
|
578 | See the examples section earlier in this document for some actual |
|
|
579 | examples. |
|
|
580 | |
400 | =back |
581 | =back |
|
|
582 | |
|
|
583 | =head1 ADVANCED TOPICS |
|
|
584 | |
|
|
585 | =head2 Choosing a backend |
|
|
586 | |
|
|
587 | So how do you decide which backend to use? Well, that's your problem to |
|
|
588 | solve, but here are some thoughts on the matter: |
|
|
589 | |
|
|
590 | =over 4 |
|
|
591 | |
|
|
592 | =item Synchronous |
|
|
593 | |
|
|
594 | The synchronous backend does not rely on any external modules (well, |
|
|
595 | except L<common::sense>, which works around a bug in how perl's warning |
|
|
596 | system works). This keeps the process very small, for example, on my |
|
|
597 | system, an empty perl interpreter uses 1492kB RSS, which becomes 2020kB |
|
|
598 | after C<use warnings; use strict> (for people who grew up with C64s around |
|
|
599 | them this is probably shocking every single time they see it). The worker |
|
|
600 | process in the first example in this document uses 1792kB. |
|
|
601 | |
|
|
602 | Since the calls are done synchronously, slow jobs will keep newer jobs |
|
|
603 | from executing. |
|
|
604 | |
|
|
605 | The synchronous backend also has no overhead due to running an event loop |
|
|
606 | - reading requests is therefore very efficient, while writing responses is |
|
|
607 | less so, as every response results in a write syscall. |
|
|
608 | |
|
|
609 | If the parent process is busy and a bit slow reading responses, the child |
|
|
610 | waits instead of processing further requests. This also limits the amount |
|
|
611 | of memory needed for buffering, as never more than one response has to be |
|
|
612 | buffered. |
|
|
613 | |
|
|
614 | The API in the child is simple - you just have to define a function that |
|
|
615 | does something and returns something. |
|
|
616 | |
|
|
617 | It's hard to use modules or code that relies on an event loop, as the |
|
|
618 | child cannot execute anything while it waits for more input. |
|
|
619 | |
|
|
620 | =item Asynchronous |
|
|
621 | |
|
|
622 | The asynchronous backend relies on L<AnyEvent>, which tries to be small, |
|
|
623 | but still comes at a price: On my system, the worker from example 1a uses |
|
|
624 | 3420kB RSS (for L<AnyEvent>, which loads L<EV>, which needs L<XSLoader> |
|
|
625 | which in turn loads a lot of other modules such as L<warnings>, L<strict>, |
|
|
626 | L<vars>, L<Exporter>...). |
|
|
627 | |
|
|
628 | It batches requests and responses reasonably efficiently, doing only as |
|
|
629 | few reads and writes as needed, but needs to poll for events via the event |
|
|
630 | loop. |
|
|
631 | |
|
|
632 | Responses are queued when the parent process is busy. This means the child |
|
|
633 | can continue to execute any queued requests. It also means that a child |
|
|
634 | might queue a lot of responses in memory when it generates them and the |
|
|
635 | parent process is slow accepting them. |
|
|
636 | |
|
|
637 | The API is not a straightforward RPC pattern - you have to call a |
|
|
638 | "done" callback to pass return values and signal completion. Also, more |
|
|
639 | importantly, the API starts jobs as fast as possible - when 1000 jobs |
|
|
640 | are queued and the jobs are slow, they will all run concurrently. The |
|
|
641 | child must implement some queueing/limiting mechanism if this causes |
|
|
642 | problems. Alternatively, the parent could limit the amount of rpc calls |
|
|
643 | that are outstanding. |
|
|
644 | |
|
|
645 | Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
|
|
646 | easy. |
|
|
647 | |
|
|
648 | =back |
|
|
649 | |
|
|
650 | =head2 Passing file descriptors |
|
|
651 | |
|
|
652 | Unlike L<AnyEvent::Fork>, this module has no in-built file handle or file |
|
|
653 | descriptor passing abilities. |
|
|
654 | |
|
|
655 | The reason is that passing file descriptors is extraordinary tricky |
|
|
656 | business, and conflicts with efficient batching of messages. |
|
|
657 | |
|
|
658 | There still is a method you can use: Create a |
|
|
659 | C<AnyEvent::Util::portable_socketpair> and C<send_fh> one half of it to |
|
|
660 | the process before you pass control to C<AnyEvent::Fork::RPC::run>. |
|
|
661 | |
|
|
662 | Whenever you want to pass a file descriptor, send an rpc request to the |
|
|
663 | child process (so it expects the descriptor), then send it over the other |
|
|
664 | half of the socketpair. The child should fetch the descriptor from the |
|
|
665 | half it has passed earlier. |
|
|
666 | |
|
|
667 | Here is some (untested) pseudocode to that effect: |
|
|
668 | |
|
|
669 | use AnyEvent::Util; |
|
|
670 | use AnyEvent::Fork::RPC; |
|
|
671 | use IO::FDPass; |
|
|
672 | |
|
|
673 | my ($s1, $s2) = AnyEvent::Util::portable_socketpair; |
|
|
674 | |
|
|
675 | my $rpc = AnyEvent::Fork |
|
|
676 | ->new |
|
|
677 | ->send_fh ($s2) |
|
|
678 | ->require ("MyWorker") |
|
|
679 | ->AnyEvent::Fork::RPC::run ("MyWorker::run" |
|
|
680 | init => "MyWorker::init", |
|
|
681 | ); |
|
|
682 | |
|
|
683 | undef $s2; # no need to keep it around |
|
|
684 | |
|
|
685 | # pass an fd |
|
|
686 | $rpc->("i'll send some fd now, please expect it!", my $cv = AE::cv); |
|
|
687 | |
|
|
688 | IO::FDPass fileno $s1, fileno $handle_to_pass; |
|
|
689 | |
|
|
690 | $cv->recv; |
|
|
691 | |
|
|
692 | The MyWorker module could look like this: |
|
|
693 | |
|
|
694 | package MyWorker; |
|
|
695 | |
|
|
696 | use IO::FDPass; |
|
|
697 | |
|
|
698 | my $s2; |
|
|
699 | |
|
|
700 | sub init { |
|
|
701 | $s2 = $_[0]; |
|
|
702 | } |
|
|
703 | |
|
|
704 | sub run { |
|
|
705 | if ($_[0] eq "i'll send some fd now, please expect it!") { |
|
|
706 | my $fd = IO::FDPass::recv fileno $s2; |
|
|
707 | ... |
|
|
708 | } |
|
|
709 | } |
|
|
710 | |
|
|
711 | Of course, this might be blocking if you pass a lot of file descriptors, |
|
|
712 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
|
|
713 | gory details. |
401 | |
714 | |
402 | =head1 SEE ALSO |
715 | =head1 SEE ALSO |
403 | |
716 | |
404 | L<AnyEvent::Fork> (to create the processes in the first place), |
717 | L<AnyEvent::Fork> (to create the processes in the first place), |
405 | L<AnyEvent::Fork::Pool> (to manage whole pools of processes). |
718 | L<AnyEvent::Fork::Pool> (to manage whole pools of processes). |