1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
|
|
4 | |
|
|
5 | THE API IS NOT FINISHED, CONSIDER THIS A TECHNOLOGY DEMO |
4 | |
6 | |
5 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
6 | |
8 | |
7 | use AnyEvent::Fork::RPC; |
9 | use AnyEvent::Fork::RPC; |
8 | # use AnyEvent::Fork is not needed |
10 | # use AnyEvent::Fork is not needed |
… | |
… | |
12 | ->require ("MyModule") |
14 | ->require ("MyModule") |
13 | ->AnyEvent::Fork::RPC::run ( |
15 | ->AnyEvent::Fork::RPC::run ( |
14 | "MyModule::server", |
16 | "MyModule::server", |
15 | ); |
17 | ); |
16 | |
18 | |
|
|
19 | use AnyEvent; |
|
|
20 | |
17 | my $cv = AE::cv; |
21 | my $cv = AE::cv; |
18 | |
22 | |
19 | $rpc->(1, 2, 3, sub { |
23 | $rpc->(1, 2, 3, sub { |
20 | print "MyModule::server returned @_\n"; |
24 | print "MyModule::server returned @_\n"; |
21 | $cv->send; |
25 | $cv->send; |
… | |
… | |
39 | Loading this module also always loads L<AnyEvent::Fork>, so you can make a |
43 | 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. |
44 | separate C<use AnyEvent::Fork> if you wish, but you don't have to. |
41 | |
45 | |
42 | =head1 EXAMPLES |
46 | =head1 EXAMPLES |
43 | |
47 | |
44 | =head2 Synchronous Backend |
48 | =head2 Example 1: Synchronous Backend |
45 | |
49 | |
46 | Here is a simple example that implements a backend that executes C<unlink> |
50 | Here is a simple example that implements a backend that executes C<unlink> |
47 | and C<rmdir> calls, and reports their status back. It also reports the |
51 | and C<rmdir> calls, and reports their status back. It also reports the |
48 | number of requests it has processed every three requests, which is clearly |
52 | number of requests it has processed every three requests, which is clearly |
49 | silly, but illustrates the use of events. |
53 | silly, but illustrates the use of events. |
50 | |
54 | |
51 | First the parent process: |
55 | First the parent process: |
52 | |
56 | |
53 | use AnyEvent; |
57 | use AnyEvent; |
54 | use AnyEvent::Fork; |
|
|
55 | use AnyEvent::Fork::RPC; |
58 | use AnyEvent::Fork::RPC; |
56 | |
59 | |
57 | my $done = AE::cv; |
60 | my $done = AE::cv; |
58 | |
61 | |
59 | my $rpc = AnyEvent::Fork |
62 | my $rpc = AnyEvent::Fork |
… | |
… | |
137 | |
140 | |
138 | And as a final remark, there is a fine module on CPAN that can |
141 | And as a final remark, there is a fine module on CPAN that can |
139 | asynchronously C<rmdir> and C<unlink> and a lot more, and more efficiently |
142 | asynchronously C<rmdir> and C<unlink> and a lot more, and more efficiently |
140 | than this example, namely L<IO::AIO>. |
143 | than this example, namely L<IO::AIO>. |
141 | |
144 | |
|
|
145 | =head3 Example 1a: the same with the asynchronous backend |
|
|
146 | |
|
|
147 | This example only shows what needs to be changed to use the async backend |
|
|
148 | instead. Doing this is not very useful, the purpose of this example is |
|
|
149 | to show the minimum amount of change that is required to go from the |
|
|
150 | synchronous to the asynchronous backend. |
|
|
151 | |
|
|
152 | To use the async backend in the previous example, you need to add the |
|
|
153 | C<async> parameter to the C<AnyEvent::Fork::RPC::run> call: |
|
|
154 | |
|
|
155 | ->AnyEvent::Fork::RPC::run ("MyWorker::run", |
|
|
156 | async => 1, |
|
|
157 | ... |
|
|
158 | |
|
|
159 | And since the function call protocol is now changed, you need to adopt |
|
|
160 | C<MyWorker::run> to the async API. |
|
|
161 | |
|
|
162 | First, you need to accept the extra initial C<$done> callback: |
|
|
163 | |
|
|
164 | sub run { |
|
|
165 | my ($done, $cmd, $path) = @_; |
|
|
166 | |
|
|
167 | And since a response is now generated when C<$done> is called, as opposed |
|
|
168 | to when the function returns, we need to call the C<$done> function with |
|
|
169 | the status: |
|
|
170 | |
|
|
171 | $done->($status or (0, "$!")); |
|
|
172 | |
|
|
173 | A few remarks are in order. First, it's quite pointless to use the async |
|
|
174 | backend for this example - but it I<is> possible. Second, you can call |
|
|
175 | C<$done> before or after returning from the function. Third, having both |
|
|
176 | returned from the function and having called the C<$done> callback, the |
|
|
177 | child process may exit at any time, so you should call C<$done> only when |
|
|
178 | you really I<are> done. |
|
|
179 | |
|
|
180 | =head2 Example 2: Asynchronous Backend |
|
|
181 | |
|
|
182 | This example implements multiple count-downs in the child, using |
|
|
183 | L<AnyEvent> timers. While this is a bit silly (one could use timers in te |
|
|
184 | parent just as well), it illustrates the ability to use AnyEvent in the |
|
|
185 | child and the fact that responses can arrive in a different order then the |
|
|
186 | requests. |
|
|
187 | |
|
|
188 | It also shows how to embed the actual child code into a C<__DATA__> |
|
|
189 | section, so it doesn't need any external files at all. |
|
|
190 | |
|
|
191 | And when your parent process is often busy, and you have stricter timing |
|
|
192 | requirements, then running timers in a child process suddenly doesn't look |
|
|
193 | so silly anymore. |
|
|
194 | |
|
|
195 | Without further ado, here is the code: |
|
|
196 | |
|
|
197 | use AnyEvent; |
|
|
198 | use AnyEvent::Fork::RPC; |
|
|
199 | |
|
|
200 | my $done = AE::cv; |
|
|
201 | |
|
|
202 | my $rpc = AnyEvent::Fork |
|
|
203 | ->new |
|
|
204 | ->require ("AnyEvent::Fork::RPC::Async") |
|
|
205 | ->eval (do { local $/; <DATA> }) |
|
|
206 | ->AnyEvent::Fork::RPC::run ("run", |
|
|
207 | async => 1, |
|
|
208 | on_error => sub { warn "FATAL: $_[0]"; exit 1 }, |
|
|
209 | on_event => sub { print $_[0] }, |
|
|
210 | on_destroy => $done, |
|
|
211 | ); |
|
|
212 | |
|
|
213 | for my $count (3, 2, 1) { |
|
|
214 | $rpc->($count, sub { |
|
|
215 | warn "job $count finished\n"; |
|
|
216 | }); |
|
|
217 | } |
|
|
218 | |
|
|
219 | undef $rpc; |
|
|
220 | |
|
|
221 | $done->recv; |
|
|
222 | |
|
|
223 | __DATA__ |
|
|
224 | |
|
|
225 | # this ends up in main, as we don't use a package declaration |
|
|
226 | |
|
|
227 | use AnyEvent; |
|
|
228 | |
|
|
229 | sub run { |
|
|
230 | my ($done, $count) = @_; |
|
|
231 | |
|
|
232 | my $n; |
|
|
233 | |
|
|
234 | AnyEvent::Fork::RPC::event "starting to count up to $count\n"; |
|
|
235 | |
|
|
236 | my $w; $w = AE::timer 1, 1, sub { |
|
|
237 | ++$n; |
|
|
238 | |
|
|
239 | AnyEvent::Fork::RPC::event "count $n of $count\n"; |
|
|
240 | |
|
|
241 | if ($n == $count) { |
|
|
242 | undef $w; |
|
|
243 | $done->(); |
|
|
244 | } |
|
|
245 | }; |
|
|
246 | } |
|
|
247 | |
|
|
248 | The parent part (the one before the C<__DATA__> section) isn't very |
|
|
249 | different from the earlier examples. It sets async mode, preloads |
|
|
250 | the backend module (so the C<AnyEvent::Fork::RPC::event> function is |
|
|
251 | declared), uses a slightly different C<on_event> handler (which we use |
|
|
252 | simply for logging purposes) and then, instead of loading a module with |
|
|
253 | the actual worker code, it C<eval>'s the code from the data section in the |
|
|
254 | child process. |
|
|
255 | |
|
|
256 | It then starts three countdowns, from 3 to 1 seconds downwards, destroys |
|
|
257 | the rpc object so the example finishes eventually, and then just waits for |
|
|
258 | the stuff to trickle in. |
|
|
259 | |
|
|
260 | The worker code uses the event function to log some progress messages, but |
|
|
261 | mostly just creates a recurring one-second timer. |
|
|
262 | |
|
|
263 | The timer callback increments a counter, logs a message, and eventually, |
|
|
264 | when the count has been reached, calls the finish callback. |
|
|
265 | |
|
|
266 | On my system, this results in the following output. Since all timers fire |
|
|
267 | at roughly the same time, the actual order isn't guaranteed, but the order |
|
|
268 | shown is very likely what you would get, too. |
|
|
269 | |
|
|
270 | starting to count up to 3 |
|
|
271 | starting to count up to 2 |
|
|
272 | starting to count up to 1 |
|
|
273 | count 1 of 3 |
|
|
274 | count 1 of 2 |
|
|
275 | count 1 of 1 |
|
|
276 | job 1 finished |
|
|
277 | count 2 of 2 |
|
|
278 | job 2 finished |
|
|
279 | count 2 of 3 |
|
|
280 | count 3 of 3 |
|
|
281 | job 3 finished |
|
|
282 | |
|
|
283 | While the overall ordering isn't guaranteed, the async backend still |
|
|
284 | guarantees that events and responses are delivered to the parent process |
|
|
285 | in the exact same ordering as they were generated in the child process. |
|
|
286 | |
|
|
287 | And unless your system is I<very> busy, it should clearly show that the |
|
|
288 | job started last will finish first, as it has the lowest count. |
|
|
289 | |
|
|
290 | This concludes the async example. Since L<AnyEvent::Fork> does not |
|
|
291 | actually fork, you are free to use about any module in the child, not just |
|
|
292 | L<AnyEvent>, but also L<IO::AIO>, or L<Tk> for example. |
|
|
293 | |
142 | =head1 PARENT PROCESS USAGE |
294 | =head1 PARENT PROCESS USAGE |
143 | |
295 | |
144 | This module exports nothing, and only implements a single function: |
296 | This module exports nothing, and only implements a single function: |
145 | |
297 | |
146 | =over 4 |
298 | =over 4 |
… | |
… | |
224 | |
376 | |
225 | The default server used in the child does all I/O blockingly, and only |
377 | The default server used in the child does all I/O blockingly, and only |
226 | allows a single RPC call to execute concurrently. |
378 | allows a single RPC call to execute concurrently. |
227 | |
379 | |
228 | Setting C<async> to a true value switches to another implementation that |
380 | Setting C<async> to a true value switches to another implementation that |
229 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls. |
381 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls (it |
|
|
382 | does not support recursion in the event loop however, blocking condvar |
|
|
383 | calls will fail). |
230 | |
384 | |
231 | The actual API in the child is documented in the section that describes |
385 | The actual API in the child is documented in the section that describes |
232 | the calling semantics of the returned C<$rpc> function. |
386 | the calling semantics of the returned C<$rpc> function. |
233 | |
387 | |
234 | If you want to pre-load the actual back-end modules to enable memory |
388 | If you want to pre-load the actual back-end modules to enable memory |
… | |
… | |
236 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
390 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
237 | |
391 | |
238 | If you use a template process and want to fork both sync and async |
392 | If you use a template process and want to fork both sync and async |
239 | children, then it is permissible to load both modules. |
393 | children, then it is permissible to load both modules. |
240 | |
394 | |
241 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
395 | =item serialiser => $string (default: $AnyEvent::Fork::RPC::STRING_SERIALISER) |
242 | |
396 | |
243 | All arguments, result data and event data have to be serialised to be |
397 | All arguments, result data and event data have to be serialised to be |
244 | transferred between the processes. For this, they have to be frozen and |
398 | transferred between the processes. For this, they have to be frozen and |
245 | thawed in both parent and child processes. |
399 | thawed in both parent and child processes. |
246 | |
400 | |
247 | By default, only octet strings can be passed between the processes, which |
401 | By default, only octet strings can be passed between the processes, which |
248 | is reasonably fast and efficient. |
402 | is reasonably fast and efficient and requires no extra modules. |
249 | |
403 | |
250 | For more complicated use cases, you can provide your own freeze and thaw |
404 | For more complicated use cases, you can provide your own freeze and thaw |
251 | functions, by specifying a string with perl source code. It's supposed to |
405 | functions, by specifying a string with perl source code. It's supposed to |
252 | return two code references when evaluated: the first receives a list of |
406 | return two code references when evaluated: the first receives a list of |
253 | perl values and must return an octet string. The second receives the octet |
407 | perl values and must return an octet string. The second receives the octet |
… | |
… | |
255 | |
409 | |
256 | If you need an external module for serialisation, then you can either |
410 | If you need an external module for serialisation, then you can either |
257 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
411 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
258 | or C<require> statement into the serialiser string. Or both. |
412 | or C<require> statement into the serialiser string. Or both. |
259 | |
413 | |
|
|
414 | Here are some examples - some of them are also available as global |
|
|
415 | variables that make them easier to use. |
|
|
416 | |
|
|
417 | =over 4 |
|
|
418 | |
|
|
419 | =item octet strings - C<$AnyEvent::Fork::RPC::STRING_SERIALISER> |
|
|
420 | |
|
|
421 | This serialiser concatenates length-prefixes octet strings, and is the |
|
|
422 | default. |
|
|
423 | |
|
|
424 | Implementation: |
|
|
425 | |
|
|
426 | ( |
|
|
427 | sub { pack "(w/a*)*", @_ }, |
|
|
428 | sub { unpack "(w/a*)*", shift } |
|
|
429 | ) |
|
|
430 | |
|
|
431 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
|
|
432 | |
|
|
433 | This serialiser creates JSON arrays - you have to make sure the L<JSON> |
|
|
434 | module is installed for this serialiser to work. It can be beneficial for |
|
|
435 | sharing when you preload the L<JSON> module in a template process. |
|
|
436 | |
|
|
437 | L<JSON> (with L<JSON::XS> installed) is slower than the octet string |
|
|
438 | serialiser, but usually much faster than L<Storable>, unless big chunks of |
|
|
439 | binary data need to be transferred. |
|
|
440 | |
|
|
441 | Implementation: |
|
|
442 | |
|
|
443 | use JSON (); |
|
|
444 | ( |
|
|
445 | sub { JSON::encode_json \@_ }, |
|
|
446 | sub { @{ JSON::decode_json shift } } |
|
|
447 | ) |
|
|
448 | |
|
|
449 | =item storable - C<$AnyEvent::Fork::RPC::STORABLE_SERIALISER> |
|
|
450 | |
|
|
451 | This serialiser uses L<Storable>, which means it has high chance of |
|
|
452 | serialising just about anything you throw at it, at the cost of having |
|
|
453 | very high overhead per operation. It also comes with perl. |
|
|
454 | |
|
|
455 | Implementation: |
|
|
456 | |
|
|
457 | use Storable (); |
|
|
458 | ( |
|
|
459 | sub { Storable::freeze \@_ }, |
|
|
460 | sub { @{ Storable::thaw shift } } |
|
|
461 | ) |
|
|
462 | |
260 | =back |
463 | =back |
261 | |
464 | |
|
|
465 | =back |
|
|
466 | |
262 | See the examples section earlier in this document for some actual examples. |
467 | See the examples section earlier in this document for some actual |
|
|
468 | examples. |
263 | |
469 | |
264 | =cut |
470 | =cut |
265 | |
471 | |
266 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
472 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
|
|
473 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
|
|
474 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
267 | |
475 | |
268 | sub run { |
476 | sub run { |
269 | my ($self, $function, %arg) = @_; |
477 | my ($self, $function, %arg) = @_; |
270 | |
478 | |
271 | my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
479 | my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
… | |
… | |
281 | # default for on_event is to raise an error |
489 | # default for on_event is to raise an error |
282 | $on_event ||= sub { $on_error->("event received, but no on_event handler") }; |
490 | $on_event ||= sub { $on_error->("event received, but no on_event handler") }; |
283 | |
491 | |
284 | my ($f, $t) = eval $serialiser; die $@ if $@; |
492 | my ($f, $t) = eval $serialiser; die $@ if $@; |
285 | |
493 | |
286 | my (@rcb, $fh, $shutdown, $wbuf, $ww, $rw); |
494 | my (@rcb, %rcb, $fh, $shutdown, $wbuf, $ww); |
287 | my ($rlen, $rbuf) = 512 - 16; |
495 | my ($rlen, $rbuf, $rw) = 512 - 16; |
288 | |
496 | |
289 | my $wcb = sub { |
497 | my $wcb = sub { |
290 | my $len = syswrite $fh, $wbuf; |
498 | my $len = syswrite $fh, $wbuf; |
291 | |
499 | |
292 | if (!defined $len) { |
500 | unless (defined $len) { |
293 | if ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
501 | if ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
294 | undef $rw; undef $ww; # it ends here |
502 | undef $rw; undef $ww; # it ends here |
295 | $on_error->("$!"); |
503 | $on_error->("$!"); |
296 | } |
504 | } |
297 | } |
505 | } |
… | |
… | |
308 | |
516 | |
309 | $self->require ($module) |
517 | $self->require ($module) |
310 | ->send_arg ($function, $arg{init}, $serialiser) |
518 | ->send_arg ($function, $arg{init}, $serialiser) |
311 | ->run ("$module\::run", sub { |
519 | ->run ("$module\::run", sub { |
312 | $fh = shift; |
520 | $fh = shift; |
|
|
521 | |
|
|
522 | my ($id, $len); |
313 | $rw = AE::io $fh, 0, sub { |
523 | $rw = AE::io $fh, 0, sub { |
314 | $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
524 | $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
315 | my $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
525 | $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
316 | |
526 | |
317 | if ($len) { |
527 | if ($len) { |
318 | while (4 <= length $rbuf) { |
528 | while (8 <= length $rbuf) { |
319 | $len = unpack "L", $rbuf; |
529 | ($id, $len) = unpack "LL", $rbuf; |
320 | 4 + $len <= length $rbuf |
530 | 8 + $len <= length $rbuf |
321 | or last; |
531 | or last; |
322 | |
532 | |
323 | my @r = $t->(substr $rbuf, 4, $len); |
533 | my @r = $t->(substr $rbuf, 8, $len); |
324 | substr $rbuf, 0, $len + 4, ""; |
534 | substr $rbuf, 0, 8 + $len, ""; |
|
|
535 | |
|
|
536 | if ($id) { |
|
|
537 | if (@rcb) { |
|
|
538 | (shift @rcb)->(@r); |
|
|
539 | } elsif (my $cb = delete $rcb{$id}) { |
|
|
540 | $cb->(@r); |
|
|
541 | } else { |
|
|
542 | undef $rw; undef $ww; |
|
|
543 | $on_error->("unexpected data from child"); |
325 | |
544 | } |
326 | if (pop @r) { |
545 | } else { |
327 | $on_event->(@r); |
546 | $on_event->(@r); |
328 | } elsif (@rcb) { |
|
|
329 | (shift @rcb)->(@r); |
|
|
330 | } else { |
|
|
331 | undef $rw; undef $ww; |
|
|
332 | $on_error->("unexpected data from child"); |
|
|
333 | } |
547 | } |
334 | } |
548 | } |
335 | } elsif (defined $len) { |
549 | } elsif (defined $len) { |
336 | undef $rw; undef $ww; # it ends here |
550 | undef $rw; undef $ww; # it ends here |
337 | |
551 | |
338 | if (@rcb) { |
552 | if (@rcb || %rcb) { |
339 | $on_error->("unexpected eof"); |
553 | $on_error->("unexpected eof"); |
340 | } else { |
554 | } else { |
341 | $on_destroy->(); |
555 | $on_destroy->() |
|
|
556 | if $on_destroy; |
342 | } |
557 | } |
343 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
558 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
344 | undef $rw; undef $ww; # it ends here |
559 | undef $rw; undef $ww; # it ends here |
345 | $on_error->("read: $!"); |
560 | $on_error->("read: $!"); |
346 | } |
561 | } |
… | |
… | |
349 | $ww ||= AE::io $fh, 1, $wcb; |
564 | $ww ||= AE::io $fh, 1, $wcb; |
350 | }); |
565 | }); |
351 | |
566 | |
352 | my $guard = Guard::guard { |
567 | my $guard = Guard::guard { |
353 | $shutdown = 1; |
568 | $shutdown = 1; |
354 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
569 | |
|
|
570 | shutdown $fh, 1 if $fh && !$ww; |
355 | }; |
571 | }; |
356 | |
572 | |
|
|
573 | my $id; |
|
|
574 | |
|
|
575 | $arg{async} |
357 | sub { |
576 | ? sub { |
358 | push @rcb, pop; |
577 | $id = ($id == 0xffffffff ? 0 : $id) + 1; |
|
|
578 | $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
359 | |
579 | |
|
|
580 | $rcb{$id} = pop; |
|
|
581 | |
360 | $guard; # keep it alive |
582 | $guard if 0; # keep it alive |
361 | |
583 | |
362 | $wbuf .= pack "L/a*", &$f; |
584 | $wbuf .= pack "LL/a*", $id, &$f; |
363 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
585 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
364 | } |
586 | } |
|
|
587 | : sub { |
|
|
588 | push @rcb, pop; |
|
|
589 | |
|
|
590 | $guard; # keep it alive |
|
|
591 | |
|
|
592 | $wbuf .= pack "L/a*", &$f; |
|
|
593 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
|
|
594 | } |
365 | } |
595 | } |
366 | |
596 | |
367 | =item $rpc->(..., $cb->(...)) |
597 | =item $rpc->(..., $cb->(...)) |
368 | |
598 | |
369 | The RPC object returned by C<AnyEvent::Fork::RPC::run> is actually a code |
599 | The RPC object returned by C<AnyEvent::Fork::RPC::run> is actually a code |
… | |
… | |
409 | See the examples section earlier in this document for some actual |
639 | See the examples section earlier in this document for some actual |
410 | examples. |
640 | examples. |
411 | |
641 | |
412 | =back |
642 | =back |
413 | |
643 | |
|
|
644 | =head1 ADVANCED TOPICS |
|
|
645 | |
|
|
646 | =head2 Choosing a backend |
|
|
647 | |
|
|
648 | So how do you decide which backend to use? Well, that's your problem to |
|
|
649 | solve, but here are some thoughts on the matter: |
|
|
650 | |
|
|
651 | =over 4 |
|
|
652 | |
|
|
653 | =item Synchronous |
|
|
654 | |
|
|
655 | The synchronous backend does not rely on any external modules (well, |
|
|
656 | except L<common::sense>, which works around a bug in how perl's warning |
|
|
657 | system works). This keeps the process very small, for example, on my |
|
|
658 | system, an empty perl interpreter uses 1492kB RSS, which becomes 2020kB |
|
|
659 | after C<use warnings; use strict> (for people who grew up with C64s around |
|
|
660 | them this is probably shocking every single time they see it). The worker |
|
|
661 | process in the first example in this document uses 1792kB. |
|
|
662 | |
|
|
663 | Since the calls are done synchronously, slow jobs will keep newer jobs |
|
|
664 | from executing. |
|
|
665 | |
|
|
666 | The synchronous backend also has no overhead due to running an event loop |
|
|
667 | - reading requests is therefore very efficient, while writing responses is |
|
|
668 | less so, as every response results in a write syscall. |
|
|
669 | |
|
|
670 | If the parent process is busy and a bit slow reading responses, the child |
|
|
671 | waits instead of processing further requests. This also limits the amount |
|
|
672 | of memory needed for buffering, as never more than one response has to be |
|
|
673 | buffered. |
|
|
674 | |
|
|
675 | The API in the child is simple - you just have to define a function that |
|
|
676 | does something and returns something. |
|
|
677 | |
|
|
678 | It's hard to use modules or code that relies on an event loop, as the |
|
|
679 | child cannot execute anything while it waits for more input. |
|
|
680 | |
|
|
681 | =item Asynchronous |
|
|
682 | |
|
|
683 | The asynchronous backend relies on L<AnyEvent>, which tries to be small, |
|
|
684 | but still comes at a price: On my system, the worker from example 1a uses |
|
|
685 | 3420kB RSS (for L<AnyEvent>, which loads L<EV>, which needs L<XSLoader> |
|
|
686 | which in turn loads a lot of other modules such as L<warnings>, L<strict>, |
|
|
687 | L<vars>, L<Exporter>...). |
|
|
688 | |
|
|
689 | It batches requests and responses reasonably efficiently, doing only as |
|
|
690 | few reads and writes as needed, but needs to poll for events via the event |
|
|
691 | loop. |
|
|
692 | |
|
|
693 | Responses are queued when the parent process is busy. This means the child |
|
|
694 | can continue to execute any queued requests. It also means that a child |
|
|
695 | might queue a lot of responses in memory when it generates them and the |
|
|
696 | parent process is slow accepting them. |
|
|
697 | |
|
|
698 | The API is not a straightforward RPC pattern - you have to call a |
|
|
699 | "done" callback to pass return values and signal completion. Also, more |
|
|
700 | importantly, the API starts jobs as fast as possible - when 1000 jobs |
|
|
701 | are queued and the jobs are slow, they will all run concurrently. The |
|
|
702 | child must implement some queueing/limiting mechanism if this causes |
|
|
703 | problems. Alternatively, the parent could limit the amount of rpc calls |
|
|
704 | that are outstanding. |
|
|
705 | |
|
|
706 | Blocking use of condvars is not supported. |
|
|
707 | |
|
|
708 | Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
|
|
709 | easy. |
|
|
710 | |
|
|
711 | =back |
|
|
712 | |
|
|
713 | =head2 Passing file descriptors |
|
|
714 | |
|
|
715 | Unlike L<AnyEvent::Fork>, this module has no in-built file handle or file |
|
|
716 | descriptor passing abilities. |
|
|
717 | |
|
|
718 | The reason is that passing file descriptors is extraordinary tricky |
|
|
719 | business, and conflicts with efficient batching of messages. |
|
|
720 | |
|
|
721 | There still is a method you can use: Create a |
|
|
722 | C<AnyEvent::Util::portable_socketpair> and C<send_fh> one half of it to |
|
|
723 | the process before you pass control to C<AnyEvent::Fork::RPC::run>. |
|
|
724 | |
|
|
725 | Whenever you want to pass a file descriptor, send an rpc request to the |
|
|
726 | child process (so it expects the descriptor), then send it over the other |
|
|
727 | half of the socketpair. The child should fetch the descriptor from the |
|
|
728 | half it has passed earlier. |
|
|
729 | |
|
|
730 | Here is some (untested) pseudocode to that effect: |
|
|
731 | |
|
|
732 | use AnyEvent::Util; |
|
|
733 | use AnyEvent::Fork::RPC; |
|
|
734 | use IO::FDPass; |
|
|
735 | |
|
|
736 | my ($s1, $s2) = AnyEvent::Util::portable_socketpair; |
|
|
737 | |
|
|
738 | my $rpc = AnyEvent::Fork |
|
|
739 | ->new |
|
|
740 | ->send_fh ($s2) |
|
|
741 | ->require ("MyWorker") |
|
|
742 | ->AnyEvent::Fork::RPC::run ("MyWorker::run" |
|
|
743 | init => "MyWorker::init", |
|
|
744 | ); |
|
|
745 | |
|
|
746 | undef $s2; # no need to keep it around |
|
|
747 | |
|
|
748 | # pass an fd |
|
|
749 | $rpc->("i'll send some fd now, please expect it!", my $cv = AE::cv); |
|
|
750 | |
|
|
751 | IO::FDPass fileno $s1, fileno $handle_to_pass; |
|
|
752 | |
|
|
753 | $cv->recv; |
|
|
754 | |
|
|
755 | The MyWorker module could look like this: |
|
|
756 | |
|
|
757 | package MyWorker; |
|
|
758 | |
|
|
759 | use IO::FDPass; |
|
|
760 | |
|
|
761 | my $s2; |
|
|
762 | |
|
|
763 | sub init { |
|
|
764 | $s2 = $_[0]; |
|
|
765 | } |
|
|
766 | |
|
|
767 | sub run { |
|
|
768 | if ($_[0] eq "i'll send some fd now, please expect it!") { |
|
|
769 | my $fd = IO::FDPass::recv fileno $s2; |
|
|
770 | ... |
|
|
771 | } |
|
|
772 | } |
|
|
773 | |
|
|
774 | Of course, this might be blocking if you pass a lot of file descriptors, |
|
|
775 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
|
|
776 | gory details. |
|
|
777 | |
|
|
778 | =head1 EXCEPTIONS |
|
|
779 | |
|
|
780 | There are no provisions whatsoever for catching exceptions at this time - |
|
|
781 | in the child, exeptions might kill the process, causing calls to be lost |
|
|
782 | and the parent encountering a fatal error. In the parent, exceptions in |
|
|
783 | the result callback will not be caught and cause undefined behaviour. |
|
|
784 | |
414 | =head1 SEE ALSO |
785 | =head1 SEE ALSO |
415 | |
786 | |
416 | L<AnyEvent::Fork> (to create the processes in the first place), |
787 | L<AnyEvent::Fork>, to create the processes in the first place. |
|
|
788 | |
417 | L<AnyEvent::Fork::Pool> (to manage whole pools of processes). |
789 | L<AnyEvent::Fork::Pool>, to manage whole pools of processes. |
418 | |
790 | |
419 | =head1 AUTHOR AND CONTACT INFORMATION |
791 | =head1 AUTHOR AND CONTACT INFORMATION |
420 | |
792 | |
421 | Marc Lehmann <schmorp@schmorp.de> |
793 | Marc Lehmann <schmorp@schmorp.de> |
422 | http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |
794 | http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |