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.14 by root, Thu Apr 18 13:11:12 2013 UTC

384synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. 384synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode.
385 385
386If you use a template process and want to fork both sync and async 386If you use a template process and want to fork both sync and async
387children, then it is permissible to load both modules. 387children, then it is permissible to load both modules.
388 388
389=item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') 389=item serialiser => $string (default: $AnyEvent::Fork::RPC::STRING_SERIALISER)
390 390
391All arguments, result data and event data have to be serialised to be 391All arguments, result data and event data have to be serialised to be
392transferred between the processes. For this, they have to be frozen and 392transferred between the processes. For this, they have to be frozen and
393thawed in both parent and child processes. 393thawed in both parent and child processes.
394 394
395By default, only octet strings can be passed between the processes, which 395By default, only octet strings can be passed between the processes, which
396is reasonably fast and efficient. 396is reasonably fast and efficient and requires no extra modules.
397 397
398For more complicated use cases, you can provide your own freeze and thaw 398For 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 399functions, by specifying a string with perl source code. It's supposed to
400return two code references when evaluated: the first receives a list of 400return two code references when evaluated: the first receives a list of
401perl values and must return an octet string. The second receives the octet 401perl values and must return an octet string. The second receives the octet
403 403
404If you need an external module for serialisation, then you can either 404If 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> 405pre-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. 406or C<require> statement into the serialiser string. Or both.
407 407
408Here are some examples - some of them are also available as global
409variables that make them easier to use.
410
411=over 4
412
413=item octet strings - C<$AnyEvent::Fork::RPC::STRING_SERIALISER>
414
415This serialiser concatenates length-prefixes octet strings, and is the
416default.
417
418Implementation:
419
420 (
421 sub { pack "(w/a*)*", @_ },
422 sub { unpack "(w/a*)*", shift }
423 )
424
425=item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER>
426
427This serialiser creates JSON arrays - you have to make sure the L<JSON>
428module is installed for this serialiser to work. It can be beneficial for
429sharing when you preload the L<JSON> module in a template process.
430
431L<JSON> (with L<JSON::XS> installed) is slower than the octet string
432serialiser, but usually much faster than L<Storable>, unless big chunks of
433binary data need to be transferred.
434
435Implementation:
436
437 use JSON ();
438 (
439 sub { JSON::encode_json \@_ },
440 sub { @{ JSON::decode_json shift } }
441 )
442
443=item storable - C<$AnyEvent::Fork::RPC::STORABLE_SERIALISER>
444
445This serialiser uses L<Storable>, which means it has high chance of
446serialising just about anything you throw at it, at the cost of having
447very high overhead per operation. It also comes with perl.
448
449Implementation:
450
451 use Storable ();
452 (
453 sub { Storable::freeze \@_ },
454 sub { @{ Storable::thaw shift } }
455 )
456
457=back
458
408=back 459=back
409 460
410See the examples section earlier in this document for some actual 461See the examples section earlier in this document for some actual
411examples. 462examples.
412 463
413=cut 464=cut
414 465
415our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; 466our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })';
467our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })';
468our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })';
416 469
417sub run { 470sub run {
418 my ($self, $function, %arg) = @_; 471 my ($self, $function, %arg) = @_;
419 472
420 my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; 473 my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines