… | |
… | |
494 | |
494 | |
495 | All arguments, result data and event data have to be serialised to be |
495 | All arguments, result data and event data have to be serialised to be |
496 | transferred between the processes. For this, they have to be frozen and |
496 | transferred between the processes. For this, they have to be frozen and |
497 | thawed in both parent and child processes. |
497 | thawed in both parent and child processes. |
498 | |
498 | |
499 | By default, only octet strings can be passed between the processes, which |
499 | By default, only octet strings can be passed between the processes, |
500 | is reasonably fast and efficient and requires no extra modules. |
500 | which is reasonably fast and efficient and requires no extra modules |
|
|
501 | (the C<AnyEvent::Fork::RPC> distribution does not provide these extra |
|
|
502 | serialiser modules). |
501 | |
503 | |
502 | For more complicated use cases, you can provide your own freeze and thaw |
504 | For more complicated use cases, you can provide your own freeze and thaw |
503 | functions, by specifying a string with perl source code. It's supposed to |
505 | functions, by specifying a string with perl source code. It's supposed to |
504 | return two code references when evaluated: the first receives a list of |
506 | return two code references when evaluated: the first receives a list of |
505 | perl values and must return an octet string. The second receives the octet |
507 | perl values and must return an octet string. The second receives the octet |
… | |
… | |
525 | ( |
527 | ( |
526 | sub { pack "(w/a*)*", @_ }, |
528 | sub { pack "(w/a*)*", @_ }, |
527 | sub { unpack "(w/a*)*", shift } |
529 | sub { unpack "(w/a*)*", shift } |
528 | ) |
530 | ) |
529 | |
531 | |
530 | =item json - C<$AnyEvent::Fork::RPC::CBOR_XS_SERIALISER> |
532 | =item cbor - C<$AnyEvent::Fork::RPC::CBOR_XS_SERIALISER> |
531 | |
533 | |
532 | This serialiser creates CBOR::XS arrays - you have to make sure the |
534 | This serialiser creates CBOR::XS arrays - you have to make sure the |
533 | L<CBOR::XS> module is installed for this serialiser to work. It can be |
535 | L<CBOR::XS> module is installed for this serialiser to work. It can be |
534 | beneficial for sharing when you preload the L<CBOR::XS> module in a template |
536 | beneficial for sharing when you preload the L<CBOR::XS> module in a template |
535 | process. |
537 | process. |
… | |
… | |
537 | L<CBOR::XS> is about as fast as the octet string serialiser, but supports |
539 | L<CBOR::XS> is about as fast as the octet string serialiser, but supports |
538 | complex data structures (similar to JSON) and is faster than any of the |
540 | complex data structures (similar to JSON) and is faster than any of the |
539 | other serialisers. If you have the L<CBOR::XS> module available, it's the |
541 | other serialisers. If you have the L<CBOR::XS> module available, it's the |
540 | best choice. |
542 | best choice. |
541 | |
543 | |
542 | Note that the CBOR::XS module supports some extensions to encode cyclic |
544 | The encoder enables C<allow_sharing> (so this serialisation method can |
543 | and self-referencing data structures, which are not enabled. You need to |
545 | encode cyclic and self-referencing data structures). |
544 | write your own serialiser to take advantage of these. |
|
|
545 | |
546 | |
546 | Implementation: |
547 | Implementation: |
547 | |
548 | |
548 | use CBOR::XS (); |
549 | use CBOR::XS (); |
549 | ( |
550 | ( |
550 | sub { CBOR::XS::encode_cbor \@_ }, |
551 | sub { CBOR::XS::encode_cbor_sharing \@_ }, |
551 | sub { @{ CBOR::XS::decode_cbor shift } } |
552 | sub { @{ CBOR::XS::decode_cbor shift } } |
552 | ) |
553 | ) |
553 | |
554 | |
554 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
555 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
555 | |
556 | |
… | |
… | |
607 | examples. |
608 | examples. |
608 | |
609 | |
609 | =cut |
610 | =cut |
610 | |
611 | |
611 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
612 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
612 | our $CBOR_XS_SERIALISER = 'use CBOR::XS (); (sub { CBOR::XS::encode_cbor \@_ }, sub { @{ CBOR::XS::decode_cbor shift } })'; |
613 | our $CBOR_XS_SERIALISER = 'use CBOR::XS (); (sub { CBOR::XS::encode_cbor_sharing \@_ }, sub { @{ CBOR::XS::decode_cbor shift } })'; |
613 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
614 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
614 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
615 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
615 | our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })'; |
616 | our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })'; |
616 | |
617 | |
617 | sub run { |
618 | sub run { |
618 | my ($self, $function, %arg) = @_; |
619 | my ($self, $function, %arg) = @_; |