… | |
… | |
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 | |
|
|
17 | use AnyEvent; |
16 | |
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"; |
… | |
… | |
372 | |
374 | |
373 | The default server used in the child does all I/O blockingly, and only |
375 | The default server used in the child does all I/O blockingly, and only |
374 | allows a single RPC call to execute concurrently. |
376 | allows a single RPC call to execute concurrently. |
375 | |
377 | |
376 | Setting C<async> to a true value switches to another implementation that |
378 | 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. |
379 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls (it |
|
|
380 | does not support recursion in the event loop however, blocking condvar |
|
|
381 | calls will fail). |
378 | |
382 | |
379 | The actual API in the child is documented in the section that describes |
383 | The actual API in the child is documented in the section that describes |
380 | the calling semantics of the returned C<$rpc> function. |
384 | the calling semantics of the returned C<$rpc> function. |
381 | |
385 | |
382 | If you want to pre-load the actual back-end modules to enable memory |
386 | 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. |
388 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
385 | |
389 | |
386 | If you use a template process and want to fork both sync and async |
390 | If you use a template process and want to fork both sync and async |
387 | children, then it is permissible to load both modules. |
391 | children, then it is permissible to load both modules. |
388 | |
392 | |
389 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
393 | =item serialiser => $string (default: $AnyEvent::Fork::RPC::STRING_SERIALISER) |
390 | |
394 | |
391 | All arguments, result data and event data have to be serialised to be |
395 | 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 |
396 | transferred between the processes. For this, they have to be frozen and |
393 | thawed in both parent and child processes. |
397 | thawed in both parent and child processes. |
394 | |
398 | |
395 | By default, only octet strings can be passed between the processes, which |
399 | By default, only octet strings can be passed between the processes, which |
396 | is reasonably fast and efficient. |
400 | is reasonably fast and efficient and requires no extra modules. |
397 | |
401 | |
398 | For more complicated use cases, you can provide your own freeze and thaw |
402 | 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 |
403 | 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 |
404 | 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 |
405 | perl values and must return an octet string. The second receives the octet |
… | |
… | |
403 | |
407 | |
404 | If you need an external module for serialisation, then you can either |
408 | 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> |
409 | 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. |
410 | or C<require> statement into the serialiser string. Or both. |
407 | |
411 | |
|
|
412 | Here are some examples - some of them are also available as global |
|
|
413 | variables that make them easier to use. |
|
|
414 | |
|
|
415 | =over 4 |
|
|
416 | |
|
|
417 | =item octet strings - C<$AnyEvent::Fork::RPC::STRING_SERIALISER> |
|
|
418 | |
|
|
419 | This serialiser concatenates length-prefixes octet strings, and is the |
|
|
420 | default. |
|
|
421 | |
|
|
422 | Implementation: |
|
|
423 | |
|
|
424 | ( |
|
|
425 | sub { pack "(w/a*)*", @_ }, |
|
|
426 | sub { unpack "(w/a*)*", shift } |
|
|
427 | ) |
|
|
428 | |
|
|
429 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
|
|
430 | |
|
|
431 | This serialiser creates JSON arrays - you have to make sure the L<JSON> |
|
|
432 | module is installed for this serialiser to work. It can be beneficial for |
|
|
433 | sharing when you preload the L<JSON> module in a template process. |
|
|
434 | |
|
|
435 | L<JSON> (with L<JSON::XS> installed) is slower than the octet string |
|
|
436 | serialiser, but usually much faster than L<Storable>, unless big chunks of |
|
|
437 | binary data need to be transferred. |
|
|
438 | |
|
|
439 | Implementation: |
|
|
440 | |
|
|
441 | use JSON (); |
|
|
442 | ( |
|
|
443 | sub { JSON::encode_json \@_ }, |
|
|
444 | sub { @{ JSON::decode_json shift } } |
|
|
445 | ) |
|
|
446 | |
|
|
447 | =item storable - C<$AnyEvent::Fork::RPC::STORABLE_SERIALISER> |
|
|
448 | |
|
|
449 | This serialiser uses L<Storable>, which means it has high chance of |
|
|
450 | serialising just about anything you throw at it, at the cost of having |
|
|
451 | very high overhead per operation. It also comes with perl. |
|
|
452 | |
|
|
453 | Implementation: |
|
|
454 | |
|
|
455 | use Storable (); |
|
|
456 | ( |
|
|
457 | sub { Storable::freeze \@_ }, |
|
|
458 | sub { @{ Storable::thaw shift } } |
|
|
459 | ) |
|
|
460 | |
|
|
461 | =back |
|
|
462 | |
408 | =back |
463 | =back |
409 | |
464 | |
410 | See the examples section earlier in this document for some actual |
465 | See the examples section earlier in this document for some actual |
411 | examples. |
466 | examples. |
412 | |
467 | |
413 | =cut |
468 | =cut |
414 | |
469 | |
415 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
470 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
|
|
471 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
|
|
472 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
416 | |
473 | |
417 | sub run { |
474 | sub run { |
418 | my ($self, $function, %arg) = @_; |
475 | my ($self, $function, %arg) = @_; |
419 | |
476 | |
420 | my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
477 | my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
… | |
… | |
489 | } |
546 | } |
490 | } elsif (defined $len) { |
547 | } elsif (defined $len) { |
491 | undef $rw; undef $ww; # it ends here |
548 | undef $rw; undef $ww; # it ends here |
492 | |
549 | |
493 | if (@rcb || %rcb) { |
550 | if (@rcb || %rcb) { |
494 | use Data::Dump;ddx[\@rcb,\%rcb];#d# |
|
|
495 | $on_error->("unexpected eof"); |
551 | $on_error->("unexpected eof"); |
496 | } else { |
552 | } else { |
497 | $on_destroy->(); |
553 | $on_destroy->(); |
498 | } |
554 | } |
499 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
555 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
… | |
… | |
641 | are queued and the jobs are slow, they will all run concurrently. The |
697 | are queued and the jobs are slow, they will all run concurrently. The |
642 | child must implement some queueing/limiting mechanism if this causes |
698 | child must implement some queueing/limiting mechanism if this causes |
643 | problems. Alternatively, the parent could limit the amount of rpc calls |
699 | problems. Alternatively, the parent could limit the amount of rpc calls |
644 | that are outstanding. |
700 | that are outstanding. |
645 | |
701 | |
|
|
702 | Blocking use of condvars is not supported. |
|
|
703 | |
646 | Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
704 | Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
647 | easy. |
705 | easy. |
648 | |
706 | |
649 | =back |
707 | =back |
650 | |
708 | |
… | |
… | |
713 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
771 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
714 | gory details. |
772 | gory details. |
715 | |
773 | |
716 | =head1 SEE ALSO |
774 | =head1 SEE ALSO |
717 | |
775 | |
718 | L<AnyEvent::Fork> (to create the processes in the first place), |
776 | L<AnyEvent::Fork>, to create the processes in the first place. |
|
|
777 | |
719 | L<AnyEvent::Fork::Pool> (to manage whole pools of processes). |
778 | L<AnyEvent::Fork::Pool>, to manage whole pools of processes. |
720 | |
779 | |
721 | =head1 AUTHOR AND CONTACT INFORMATION |
780 | =head1 AUTHOR AND CONTACT INFORMATION |
722 | |
781 | |
723 | Marc Lehmann <schmorp@schmorp.de> |
782 | Marc Lehmann <schmorp@schmorp.de> |
724 | http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |
783 | http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |