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 BETA RELEASE |
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 |
… | |
… | |
11 | ->new |
13 | ->new |
12 | ->require ("MyModule") |
14 | ->require ("MyModule") |
13 | ->AnyEvent::Fork::RPC::run ( |
15 | ->AnyEvent::Fork::RPC::run ( |
14 | "MyModule::server", |
16 | "MyModule::server", |
15 | ); |
17 | ); |
|
|
18 | |
|
|
19 | use AnyEvent; |
16 | |
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"; |
… | |
… | |
301 | |
305 | |
302 | use Errno (); |
306 | use Errno (); |
303 | use Guard (); |
307 | use Guard (); |
304 | |
308 | |
305 | use AnyEvent; |
309 | use AnyEvent; |
|
|
310 | # explicit version on next line, as some cpan-testers test with the 0.1 version, |
|
|
311 | # ignoring dependencies, and this line will at least give a clear indication of that. |
306 | use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
312 | use AnyEvent::Fork 0.6; # we don't actually depend on it, this is for convenience |
307 | |
313 | |
308 | our $VERSION = 0.1; |
314 | our $VERSION = 0.2; |
309 | |
315 | |
310 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
316 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
311 | |
317 | |
312 | The traditional way to call it. But it is way cooler to call it in the |
318 | The traditional way to call it. But it is way cooler to call it in the |
313 | following way: |
319 | following way: |
… | |
… | |
372 | |
378 | |
373 | The default server used in the child does all I/O blockingly, and only |
379 | The default server used in the child does all I/O blockingly, and only |
374 | allows a single RPC call to execute concurrently. |
380 | allows a single RPC call to execute concurrently. |
375 | |
381 | |
376 | Setting C<async> to a true value switches to another implementation that |
382 | Setting C<async> to a true value switches to another implementation that |
377 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls. |
383 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls (it |
|
|
384 | does not support recursion in the event loop however, blocking condvar |
|
|
385 | calls will fail). |
378 | |
386 | |
379 | The actual API in the child is documented in the section that describes |
387 | The actual API in the child is documented in the section that describes |
380 | the calling semantics of the returned C<$rpc> function. |
388 | the calling semantics of the returned C<$rpc> function. |
381 | |
389 | |
382 | If you want to pre-load the actual back-end modules to enable memory |
390 | If you want to pre-load the actual back-end modules to enable memory |
… | |
… | |
384 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
392 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
385 | |
393 | |
386 | If you use a template process and want to fork both sync and async |
394 | If you use a template process and want to fork both sync and async |
387 | children, then it is permissible to load both modules. |
395 | children, then it is permissible to load both modules. |
388 | |
396 | |
389 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
397 | =item serialiser => $string (default: $AnyEvent::Fork::RPC::STRING_SERIALISER) |
390 | |
398 | |
391 | All arguments, result data and event data have to be serialised to be |
399 | All arguments, result data and event data have to be serialised to be |
392 | transferred between the processes. For this, they have to be frozen and |
400 | transferred between the processes. For this, they have to be frozen and |
393 | thawed in both parent and child processes. |
401 | thawed in both parent and child processes. |
394 | |
402 | |
395 | By default, only octet strings can be passed between the processes, which |
403 | By default, only octet strings can be passed between the processes, which |
396 | is reasonably fast and efficient. |
404 | is reasonably fast and efficient and requires no extra modules. |
397 | |
405 | |
398 | For more complicated use cases, you can provide your own freeze and thaw |
406 | For more complicated use cases, you can provide your own freeze and thaw |
399 | functions, by specifying a string with perl source code. It's supposed to |
407 | functions, by specifying a string with perl source code. It's supposed to |
400 | return two code references when evaluated: the first receives a list of |
408 | return two code references when evaluated: the first receives a list of |
401 | perl values and must return an octet string. The second receives the octet |
409 | perl values and must return an octet string. The second receives the octet |
… | |
… | |
403 | |
411 | |
404 | If you need an external module for serialisation, then you can either |
412 | If you need an external module for serialisation, then you can either |
405 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
413 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
406 | or C<require> statement into the serialiser string. Or both. |
414 | or C<require> statement into the serialiser string. Or both. |
407 | |
415 | |
|
|
416 | Here are some examples - some of them are also available as global |
|
|
417 | variables that make them easier to use. |
|
|
418 | |
|
|
419 | =over 4 |
|
|
420 | |
|
|
421 | =item octet strings - C<$AnyEvent::Fork::RPC::STRING_SERIALISER> |
|
|
422 | |
|
|
423 | This serialiser concatenates length-prefixes octet strings, and is the |
|
|
424 | default. |
|
|
425 | |
|
|
426 | Implementation: |
|
|
427 | |
|
|
428 | ( |
|
|
429 | sub { pack "(w/a*)*", @_ }, |
|
|
430 | sub { unpack "(w/a*)*", shift } |
|
|
431 | ) |
|
|
432 | |
|
|
433 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
|
|
434 | |
|
|
435 | This serialiser creates JSON arrays - you have to make sure the L<JSON> |
|
|
436 | module is installed for this serialiser to work. It can be beneficial for |
|
|
437 | sharing when you preload the L<JSON> module in a template process. |
|
|
438 | |
|
|
439 | L<JSON> (with L<JSON::XS> installed) is slower than the octet string |
|
|
440 | serialiser, but usually much faster than L<Storable>, unless big chunks of |
|
|
441 | binary data need to be transferred. |
|
|
442 | |
|
|
443 | Implementation: |
|
|
444 | |
|
|
445 | use JSON (); |
|
|
446 | ( |
|
|
447 | sub { JSON::encode_json \@_ }, |
|
|
448 | sub { @{ JSON::decode_json shift } } |
|
|
449 | ) |
|
|
450 | |
|
|
451 | =item storable - C<$AnyEvent::Fork::RPC::STORABLE_SERIALISER> |
|
|
452 | |
|
|
453 | This serialiser uses L<Storable>, which means it has high chance of |
|
|
454 | serialising just about anything you throw at it, at the cost of having |
|
|
455 | very high overhead per operation. It also comes with perl. |
|
|
456 | |
|
|
457 | Implementation: |
|
|
458 | |
|
|
459 | use Storable (); |
|
|
460 | ( |
|
|
461 | sub { Storable::freeze \@_ }, |
|
|
462 | sub { @{ Storable::thaw shift } } |
|
|
463 | ) |
|
|
464 | |
|
|
465 | =back |
|
|
466 | |
408 | =back |
467 | =back |
409 | |
468 | |
410 | See the examples section earlier in this document for some actual |
469 | See the examples section earlier in this document for some actual |
411 | examples. |
470 | examples. |
412 | |
471 | |
413 | =cut |
472 | =cut |
414 | |
473 | |
415 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
474 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
|
|
475 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
|
|
476 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
416 | |
477 | |
417 | sub run { |
478 | sub run { |
418 | my ($self, $function, %arg) = @_; |
479 | my ($self, $function, %arg) = @_; |
419 | |
480 | |
420 | my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
481 | my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
… | |
… | |
491 | undef $rw; undef $ww; # it ends here |
552 | undef $rw; undef $ww; # it ends here |
492 | |
553 | |
493 | if (@rcb || %rcb) { |
554 | if (@rcb || %rcb) { |
494 | $on_error->("unexpected eof"); |
555 | $on_error->("unexpected eof"); |
495 | } else { |
556 | } else { |
496 | $on_destroy->(); |
557 | $on_destroy->() |
|
|
558 | if $on_destroy; |
497 | } |
559 | } |
498 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
560 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
499 | undef $rw; undef $ww; # it ends here |
561 | undef $rw; undef $ww; # it ends here |
500 | $on_error->("read: $!"); |
562 | $on_error->("read: $!"); |
501 | } |
563 | } |
… | |
… | |
504 | $ww ||= AE::io $fh, 1, $wcb; |
566 | $ww ||= AE::io $fh, 1, $wcb; |
505 | }); |
567 | }); |
506 | |
568 | |
507 | my $guard = Guard::guard { |
569 | my $guard = Guard::guard { |
508 | $shutdown = 1; |
570 | $shutdown = 1; |
509 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
571 | |
|
|
572 | shutdown $fh, 1 if $fh && !$ww; |
510 | }; |
573 | }; |
511 | |
574 | |
512 | my $id; |
575 | my $id; |
513 | |
576 | |
514 | $arg{async} |
577 | $arg{async} |
… | |
… | |
516 | $id = ($id == 0xffffffff ? 0 : $id) + 1; |
579 | $id = ($id == 0xffffffff ? 0 : $id) + 1; |
517 | $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
580 | $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
518 | |
581 | |
519 | $rcb{$id} = pop; |
582 | $rcb{$id} = pop; |
520 | |
583 | |
521 | $guard; # keep it alive |
584 | $guard if 0; # keep it alive |
522 | |
585 | |
523 | $wbuf .= pack "LL/a*", $id, &$f; |
586 | $wbuf .= pack "LL/a*", $id, &$f; |
524 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
587 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
525 | } |
588 | } |
526 | : sub { |
589 | : sub { |
… | |
… | |
640 | are queued and the jobs are slow, they will all run concurrently. The |
703 | are queued and the jobs are slow, they will all run concurrently. The |
641 | child must implement some queueing/limiting mechanism if this causes |
704 | child must implement some queueing/limiting mechanism if this causes |
642 | problems. Alternatively, the parent could limit the amount of rpc calls |
705 | problems. Alternatively, the parent could limit the amount of rpc calls |
643 | that are outstanding. |
706 | that are outstanding. |
644 | |
707 | |
|
|
708 | Blocking use of condvars is not supported. |
|
|
709 | |
645 | Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
710 | Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
646 | easy. |
711 | easy. |
647 | |
712 | |
648 | =back |
713 | =back |
649 | |
714 | |
… | |
… | |
710 | |
775 | |
711 | Of course, this might be blocking if you pass a lot of file descriptors, |
776 | 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 |
777 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
713 | gory details. |
778 | gory details. |
714 | |
779 | |
|
|
780 | =head1 EXCEPTIONS |
|
|
781 | |
|
|
782 | There are no provisions whatsoever for catching exceptions at this time - |
|
|
783 | in the child, exeptions might kill the process, causing calls to be lost |
|
|
784 | and the parent encountering a fatal error. In the parent, exceptions in |
|
|
785 | the result callback will not be caught and cause undefined behaviour. |
|
|
786 | |
715 | =head1 SEE ALSO |
787 | =head1 SEE ALSO |
716 | |
788 | |
717 | L<AnyEvent::Fork> (to create the processes in the first place), |
789 | L<AnyEvent::Fork>, to create the processes in the first place. |
|
|
790 | |
718 | L<AnyEvent::Fork::Pool> (to manage whole pools of processes). |
791 | L<AnyEvent::Fork::Pool>, to manage whole pools of processes. |
719 | |
792 | |
720 | =head1 AUTHOR AND CONTACT INFORMATION |
793 | =head1 AUTHOR AND CONTACT INFORMATION |
721 | |
794 | |
722 | Marc Lehmann <schmorp@schmorp.de> |
795 | Marc Lehmann <schmorp@schmorp.de> |
723 | http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |
796 | http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |