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