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

Comparing AnyEvent-Fork-RPC/RPC.pm (file contents):
Revision 1.13 by root, Thu Apr 18 11:11:26 2013 UTC vs.
Revision 1.28 by root, Sun Apr 28 15:48:31 2013 UTC

2 2
3AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork 3AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::Fork;
7 use AnyEvent::Fork::RPC; 8 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 (
14 "MyModule::server", 14 "MyModule::server",
15 ); 15 );
16 16
17 use AnyEvent;
18
17 my $cv = AE::cv; 19 my $cv = AE::cv;
18 20
19 $rpc->(1, 2, 3, sub { 21 $rpc->(1, 2, 3, sub {
20 print "MyModule::server returned @_\n"; 22 print "MyModule::server returned @_\n";
21 $cv->send; 23 $cv->send;
24 $cv->recv; 26 $cv->recv;
25 27
26=head1 DESCRIPTION 28=head1 DESCRIPTION
27 29
28This module implements a simple RPC protocol and backend for processes 30This module implements a simple RPC protocol and backend for processes
29created via L<AnyEvent::Fork>, allowing you to call a function in the 31created via L<AnyEvent::Fork> (or L<AnyEvent::Fork::Remote>), allowing you
30child process and receive its return values (up to 4GB serialised). 32to call a function in the child process and receive its return values (up
33to 4GB serialised).
31 34
32It implements two different backends: a synchronous one that works like a 35It implements two different backends: a synchronous one that works like a
33normal function call, and an asynchronous one that can run multiple jobs 36normal function call, and an asynchronous one that can run multiple jobs
34concurrently in the child, using AnyEvent. 37concurrently in the child, using AnyEvent.
35 38
36It also implements an asynchronous event mechanism from the child to the 39It also implements an asynchronous event mechanism from the child to the
37parent, that could be used for progress indications or other information. 40parent, that could be used for progress indications or other information.
38
39Loading this module also always loads L<AnyEvent::Fork>, so you can make a
40separate C<use AnyEvent::Fork> if you wish, but you don't have to.
41 41
42=head1 EXAMPLES 42=head1 EXAMPLES
43 43
44=head2 Example 1: Synchronous Backend 44=head2 Example 1: Synchronous Backend
45 45
49silly, but illustrates the use of events. 49silly, but illustrates the use of events.
50 50
51First the parent process: 51First the parent process:
52 52
53 use AnyEvent; 53 use AnyEvent;
54 use AnyEvent::Fork;
54 use AnyEvent::Fork::RPC; 55 use AnyEvent::Fork::RPC;
55 56
56 my $done = AE::cv; 57 my $done = AE::cv;
57 58
58 my $rpc = AnyEvent::Fork 59 my $rpc = AnyEvent::Fork
189so silly anymore. 190so silly anymore.
190 191
191Without further ado, here is the code: 192Without further ado, here is the code:
192 193
193 use AnyEvent; 194 use AnyEvent;
195 use AnyEvent::Fork;
194 use AnyEvent::Fork::RPC; 196 use AnyEvent::Fork::RPC;
195 197
196 my $done = AE::cv; 198 my $done = AE::cv;
197 199
198 my $rpc = AnyEvent::Fork 200 my $rpc = AnyEvent::Fork
301 303
302use Errno (); 304use Errno ();
303use Guard (); 305use Guard ();
304 306
305use AnyEvent; 307use AnyEvent;
306use AnyEvent::Fork; # we don't actually depend on it, this is for convenience
307 308
308our $VERSION = 0.1; 309our $VERSION = 1.1;
309 310
310=item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] 311=item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...]
311 312
312The traditional way to call it. But it is way cooler to call it in the 313The traditional way to call it. But it is way cooler to call it in the
313following way: 314following way:
372 373
373The default server used in the child does all I/O blockingly, and only 374The default server used in the child does all I/O blockingly, and only
374allows a single RPC call to execute concurrently. 375allows a single RPC call to execute concurrently.
375 376
376Setting C<async> to a true value switches to another implementation that 377Setting C<async> to a true value switches to another implementation that
377uses L<AnyEvent> in the child and allows multiple concurrent RPC calls. 378uses L<AnyEvent> in the child and allows multiple concurrent RPC calls (it
379does not support recursion in the event loop however, blocking condvar
380calls will fail).
378 381
379The actual API in the child is documented in the section that describes 382The actual API in the child is documented in the section that describes
380the calling semantics of the returned C<$rpc> function. 383the calling semantics of the returned C<$rpc> function.
381 384
382If you want to pre-load the actual back-end modules to enable memory 385If you want to pre-load the actual back-end modules to enable memory
384synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. 387synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode.
385 388
386If you use a template process and want to fork both sync and async 389If you use a template process and want to fork both sync and async
387children, then it is permissible to load both modules. 390children, then it is permissible to load both modules.
388 391
389=item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') 392=item serialiser => $string (default: $AnyEvent::Fork::RPC::STRING_SERIALISER)
390 393
391All arguments, result data and event data have to be serialised to be 394All arguments, result data and event data have to be serialised to be
392transferred between the processes. For this, they have to be frozen and 395transferred between the processes. For this, they have to be frozen and
393thawed in both parent and child processes. 396thawed in both parent and child processes.
394 397
395By default, only octet strings can be passed between the processes, which 398By default, only octet strings can be passed between the processes, which
396is reasonably fast and efficient. 399is reasonably fast and efficient and requires no extra modules.
397 400
398For more complicated use cases, you can provide your own freeze and thaw 401For more complicated use cases, you can provide your own freeze and thaw
399functions, by specifying a string with perl source code. It's supposed to 402functions, by specifying a string with perl source code. It's supposed to
400return two code references when evaluated: the first receives a list of 403return two code references when evaluated: the first receives a list of
401perl values and must return an octet string. The second receives the octet 404perl values and must return an octet string. The second receives the octet
403 406
404If you need an external module for serialisation, then you can either 407If you need an external module for serialisation, then you can either
405pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> 408pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use>
406or C<require> statement into the serialiser string. Or both. 409or C<require> statement into the serialiser string. Or both.
407 410
411Here are some examples - some of them are also available as global
412variables that make them easier to use.
413
414=over 4
415
416=item octet strings - C<$AnyEvent::Fork::RPC::STRING_SERIALISER>
417
418This serialiser concatenates length-prefixes octet strings, and is the
419default.
420
421Implementation:
422
423 (
424 sub { pack "(w/a*)*", @_ },
425 sub { unpack "(w/a*)*", shift }
426 )
427
428=item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER>
429
430This serialiser creates JSON arrays - you have to make sure the L<JSON>
431module is installed for this serialiser to work. It can be beneficial for
432sharing when you preload the L<JSON> module in a template process.
433
434L<JSON> (with L<JSON::XS> installed) is slower than the octet string
435serialiser, but usually much faster than L<Storable>, unless big chunks of
436binary data need to be transferred.
437
438Implementation:
439
440 use JSON ();
441 (
442 sub { JSON::encode_json \@_ },
443 sub { @{ JSON::decode_json shift } }
444 )
445
446=item storable - C<$AnyEvent::Fork::RPC::STORABLE_SERIALISER>
447
448This serialiser uses L<Storable>, which means it has high chance of
449serialising just about anything you throw at it, at the cost of having
450very high overhead per operation. It also comes with perl.
451
452Implementation:
453
454 use Storable ();
455 (
456 sub { Storable::freeze \@_ },
457 sub { @{ Storable::thaw shift } }
458 )
459
460=item portable storable - C<$AnyEvent::Fork::RPC::NSTORABLE_SERIALISER>
461
462This serialiser also uses L<Storable>, but uses it's "network" format
463to serialise data, which makes it possible to talk to incompatible
464perl versions (for example, when talking to a process created with
465L<AnyEvent::Fork::Remote>).
466
467Implementation:
468
469 use Storable ();
470 (
471 sub { Storable::nfreeze \@_ },
472 sub { @{ Storable::thaw shift } }
473 )
474
475=back
476
408=back 477=back
409 478
410See the examples section earlier in this document for some actual 479See the examples section earlier in this document for some actual
411examples. 480examples.
412 481
413=cut 482=cut
414 483
415our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; 484our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })';
485our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })';
486our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })';
487our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })';
416 488
417sub run { 489sub run {
418 my ($self, $function, %arg) = @_; 490 my ($self, $function, %arg) = @_;
419 491
420 my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; 492 my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER;
465 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; 537 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
466 $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; 538 $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf;
467 539
468 if ($len) { 540 if ($len) {
469 while (8 <= length $rbuf) { 541 while (8 <= length $rbuf) {
470 ($id, $len) = unpack "LL", $rbuf; 542 ($id, $len) = unpack "NN", $rbuf;
471 8 + $len <= length $rbuf 543 8 + $len <= length $rbuf
472 or last; 544 or last;
473 545
474 my @r = $t->(substr $rbuf, 8, $len); 546 my @r = $t->(substr $rbuf, 8, $len);
475 substr $rbuf, 0, 8 + $len, ""; 547 substr $rbuf, 0, 8 + $len, "";
491 undef $rw; undef $ww; # it ends here 563 undef $rw; undef $ww; # it ends here
492 564
493 if (@rcb || %rcb) { 565 if (@rcb || %rcb) {
494 $on_error->("unexpected eof"); 566 $on_error->("unexpected eof");
495 } else { 567 } else {
496 $on_destroy->(); 568 $on_destroy->()
569 if $on_destroy;
497 } 570 }
498 } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { 571 } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) {
499 undef $rw; undef $ww; # it ends here 572 undef $rw; undef $ww; # it ends here
500 $on_error->("read: $!"); 573 $on_error->("read: $!");
501 } 574 }
504 $ww ||= AE::io $fh, 1, $wcb; 577 $ww ||= AE::io $fh, 1, $wcb;
505 }); 578 });
506 579
507 my $guard = Guard::guard { 580 my $guard = Guard::guard {
508 $shutdown = 1; 581 $shutdown = 1;
509 $ww ||= $fh && AE::io $fh, 1, $wcb; 582
583 shutdown $fh, 1 if $fh && !$ww;
510 }; 584 };
511 585
512 my $id; 586 my $id;
513 587
514 $arg{async} 588 $arg{async}
516 $id = ($id == 0xffffffff ? 0 : $id) + 1; 590 $id = ($id == 0xffffffff ? 0 : $id) + 1;
517 $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops 591 $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops
518 592
519 $rcb{$id} = pop; 593 $rcb{$id} = pop;
520 594
521 $guard; # keep it alive 595 $guard if 0; # keep it alive
522 596
523 $wbuf .= pack "LL/a*", $id, &$f; 597 $wbuf .= pack "NN/a*", $id, &$f;
524 $ww ||= $fh && AE::io $fh, 1, $wcb; 598 $ww ||= $fh && AE::io $fh, 1, $wcb;
525 } 599 }
526 : sub { 600 : sub {
527 push @rcb, pop; 601 push @rcb, pop;
528 602
529 $guard; # keep it alive 603 $guard; # keep it alive
530 604
531 $wbuf .= pack "L/a*", &$f; 605 $wbuf .= pack "N/a*", &$f;
532 $ww ||= $fh && AE::io $fh, 1, $wcb; 606 $ww ||= $fh && AE::io $fh, 1, $wcb;
533 } 607 }
534} 608}
535 609
536=item $rpc->(..., $cb->(...)) 610=item $rpc->(..., $cb->(...))
640are queued and the jobs are slow, they will all run concurrently. The 714are queued and the jobs are slow, they will all run concurrently. The
641child must implement some queueing/limiting mechanism if this causes 715child must implement some queueing/limiting mechanism if this causes
642problems. Alternatively, the parent could limit the amount of rpc calls 716problems. Alternatively, the parent could limit the amount of rpc calls
643that are outstanding. 717that are outstanding.
644 718
719Blocking use of condvars is not supported.
720
645Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is 721Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is
646easy. 722easy.
647 723
648=back 724=back
649 725
665half it has passed earlier. 741half it has passed earlier.
666 742
667Here is some (untested) pseudocode to that effect: 743Here is some (untested) pseudocode to that effect:
668 744
669 use AnyEvent::Util; 745 use AnyEvent::Util;
746 use AnyEvent::Fork;
670 use AnyEvent::Fork::RPC; 747 use AnyEvent::Fork::RPC;
671 use IO::FDPass; 748 use IO::FDPass;
672 749
673 my ($s1, $s2) = AnyEvent::Util::portable_socketpair; 750 my ($s1, $s2) = AnyEvent::Util::portable_socketpair;
674 751
710 787
711Of course, this might be blocking if you pass a lot of file descriptors, 788Of course, this might be blocking if you pass a lot of file descriptors,
712so you might want to look into L<AnyEvent::FDpasser> which can handle the 789so you might want to look into L<AnyEvent::FDpasser> which can handle the
713gory details. 790gory details.
714 791
792=head1 EXCEPTIONS
793
794There are no provisions whatsoever for catching exceptions at this time -
795in the child, exeptions might kill the process, causing calls to be lost
796and the parent encountering a fatal error. In the parent, exceptions in
797the result callback will not be caught and cause undefined behaviour.
798
715=head1 SEE ALSO 799=head1 SEE ALSO
716 800
717L<AnyEvent::Fork> (to create the processes in the first place), 801L<AnyEvent::Fork>, to create the processes in the first place.
802
803L<AnyEvent::Fork::Remote>, likewise, but helpful for remote processes.
804
718L<AnyEvent::Fork::Pool> (to manage whole pools of processes). 805L<AnyEvent::Fork::Pool>, to manage whole pools of processes.
719 806
720=head1 AUTHOR AND CONTACT INFORMATION 807=head1 AUTHOR AND CONTACT INFORMATION
721 808
722 Marc Lehmann <schmorp@schmorp.de> 809 Marc Lehmann <schmorp@schmorp.de>
723 http://software.schmorp.de/pkg/AnyEvent-Fork-RPC 810 http://software.schmorp.de/pkg/AnyEvent-Fork-RPC

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines