… | |
… | |
170 | child process may exit at any time, so you should call $done only when |
170 | child process may exit at any time, so you should call $done only when |
171 | you really *are* done. |
171 | you really *are* done. |
172 | |
172 | |
173 | Example 2: Asynchronous Backend |
173 | Example 2: Asynchronous Backend |
174 | This example implements multiple count-downs in the child, using |
174 | This example implements multiple count-downs in the child, using |
175 | AnyEvent timers. While this is a bit silly (one could use timers in te |
175 | AnyEvent timers. While this is a bit silly (one could use timers in the |
176 | parent just as well), it illustrates the ability to use AnyEvent in the |
176 | parent just as well), it illustrates the ability to use AnyEvent in the |
177 | child and the fact that responses can arrive in a different order then |
177 | child and the fact that responses can arrive in a different order then |
178 | the requests. |
178 | the requests. |
179 | |
179 | |
180 | It also shows how to embed the actual child code into a "__DATA__" |
180 | It also shows how to embed the actual child code into a "__DATA__" |
… | |
… | |
435 | the synchronous backend. |
435 | the synchronous backend. |
436 | |
436 | |
437 | By overriding this you can prolong the life of a RPC process |
437 | By overriding this you can prolong the life of a RPC process |
438 | after e.g. the parent has exited by running the event loop in |
438 | after e.g. the parent has exited by running the event loop in |
439 | the provided function (or simply calling it, for example, when |
439 | the provided function (or simply calling it, for example, when |
440 | your child process uses EV you could provide EV::loop as "done" |
440 | your child process uses EV you could provide EV::run as "done" |
441 | function). |
441 | function). |
442 | |
442 | |
443 | Of course, in that case you are responsible for exiting at the |
443 | Of course, in that case you are responsible for exiting at the |
444 | appropriate time and not returning from |
444 | appropriate time and not returning from |
445 | |
445 | |
… | |
… | |
469 | to be transferred between the processes. For this, they have to |
469 | to be transferred between the processes. For this, they have to |
470 | be frozen and thawed in both parent and child processes. |
470 | be frozen and thawed in both parent and child processes. |
471 | |
471 | |
472 | By default, only octet strings can be passed between the |
472 | By default, only octet strings can be passed between the |
473 | processes, which is reasonably fast and efficient and requires |
473 | processes, which is reasonably fast and efficient and requires |
474 | no extra modules. |
474 | no extra modules (the "AnyEvent::Fork::RPC" distribution does |
|
|
475 | not provide these extra serialiser modules). |
475 | |
476 | |
476 | For more complicated use cases, you can provide your own freeze |
477 | For more complicated use cases, you can provide your own freeze |
477 | and thaw functions, by specifying a string with perl source |
478 | and thaw functions, by specifying a string with perl source |
478 | code. It's supposed to return two code references when |
479 | code. It's supposed to return two code references when |
479 | evaluated: the first receives a list of perl values and must |
480 | evaluated: the first receives a list of perl values and must |
… | |
… | |
483 | If you need an external module for serialisation, then you can |
484 | If you need an external module for serialisation, then you can |
484 | either pre-load it into your AnyEvent::Fork process, or you can |
485 | either pre-load it into your AnyEvent::Fork process, or you can |
485 | add a "use" or "require" statement into the serialiser string. |
486 | add a "use" or "require" statement into the serialiser string. |
486 | Or both. |
487 | Or both. |
487 | |
488 | |
488 | Here are some examples - some of them are also available as |
489 | Here are some examples - all of them are also available as |
489 | global variables that make them easier to use. |
490 | global variables that make them easier to use. |
490 | |
491 | |
491 | octet strings - $AnyEvent::Fork::RPC::STRING_SERIALISER |
492 | $AnyEvent::Fork::RPC::STRING_SERIALISER - octet strings only |
492 | This serialiser concatenates length-prefixes octet strings, |
493 | This serialiser (currently the default) concatenates |
493 | and is the default. That means you can only pass (and |
494 | length-prefixes octet strings, and is the default. That |
494 | return) strings containing character codes 0-255. |
495 | means you can only pass (and return) strings containing |
|
|
496 | character codes 0-255. |
|
|
497 | |
|
|
498 | The main advantages of this serialiser are the high speed |
|
|
499 | and that it doesn't need another module. The main |
|
|
500 | disadvantage is that you are very limited in what you can |
|
|
501 | pass - only octet strings. |
495 | |
502 | |
496 | Implementation: |
503 | Implementation: |
497 | |
504 | |
498 | ( |
505 | ( |
499 | sub { pack "(w/a*)*", @_ }, |
506 | sub { pack "(w/a*)*", @_ }, |
500 | sub { unpack "(w/a*)*", shift } |
507 | sub { unpack "(w/a*)*", shift } |
501 | ) |
508 | ) |
502 | |
509 | |
|
|
510 | $AnyEvent::Fork::RPC::CBOR_XS_SERIALISER - uses CBOR::XS |
|
|
511 | This serialiser creates CBOR::XS arrays - you have to make |
|
|
512 | sure the CBOR::XS module is installed for this serialiser to |
|
|
513 | work. It can be beneficial for sharing when you preload the |
|
|
514 | CBOR::XS module in a template process. |
|
|
515 | |
|
|
516 | CBOR::XS is about as fast as the octet string serialiser, |
|
|
517 | but supports complex data structures (similar to JSON) and |
|
|
518 | is faster than any of the other serialisers. If you have the |
|
|
519 | CBOR::XS module available, it's the best choice. |
|
|
520 | |
|
|
521 | The encoder enables "allow_sharing" (so this serialisation |
|
|
522 | method can encode cyclic and self-referencing data |
|
|
523 | structures). |
|
|
524 | |
|
|
525 | Implementation: |
|
|
526 | |
|
|
527 | use CBOR::XS (); |
|
|
528 | ( |
|
|
529 | sub { CBOR::XS::encode_cbor_sharing \@_ }, |
|
|
530 | sub { @{ CBOR::XS::decode_cbor shift } } |
|
|
531 | ) |
|
|
532 | |
503 | json - $AnyEvent::Fork::RPC::JSON_SERIALISER |
533 | $AnyEvent::Fork::RPC::JSON_SERIALISER - uses JSON::XS or JSON |
504 | This serialiser creates JSON arrays - you have to make sure |
534 | This serialiser creates JSON arrays - you have to make sure |
505 | the JSON module is installed for this serialiser to work. It |
535 | the JSON module is installed for this serialiser to work. It |
506 | can be beneficial for sharing when you preload the JSON |
536 | can be beneficial for sharing when you preload the JSON |
507 | module in a template process. |
537 | module in a template process. |
508 | |
538 | |
… | |
… | |
516 | ( |
546 | ( |
517 | sub { JSON::encode_json \@_ }, |
547 | sub { JSON::encode_json \@_ }, |
518 | sub { @{ JSON::decode_json shift } } |
548 | sub { @{ JSON::decode_json shift } } |
519 | ) |
549 | ) |
520 | |
550 | |
521 | storable - $AnyEvent::Fork::RPC::STORABLE_SERIALISER |
551 | $AnyEvent::Fork::RPC::STORABLE_SERIALISER - Storable |
522 | This serialiser uses Storable, which means it has high |
552 | This serialiser uses Storable, which means it has high |
523 | chance of serialising just about anything you throw at it, |
553 | chance of serialising just about anything you throw at it, |
524 | at the cost of having very high overhead per operation. It |
554 | at the cost of having very high overhead per operation. It |
525 | also comes with perl. It should be used when you need to |
555 | also comes with perl. It should be used when you need to |
526 | serialise complex data structures. |
556 | serialise complex data structures. |
… | |
… | |
531 | ( |
561 | ( |
532 | sub { Storable::freeze \@_ }, |
562 | sub { Storable::freeze \@_ }, |
533 | sub { @{ Storable::thaw shift } } |
563 | sub { @{ Storable::thaw shift } } |
534 | ) |
564 | ) |
535 | |
565 | |
536 | portable storable - $AnyEvent::Fork::RPC::NSTORABLE_SERIALISER |
566 | $AnyEvent::Fork::RPC::NSTORABLE_SERIALISER - portable Storable |
537 | This serialiser also uses Storable, but uses it's "network" |
567 | This serialiser also uses Storable, but uses it's "network" |
538 | format to serialise data, which makes it possible to talk to |
568 | format to serialise data, which makes it possible to talk to |
539 | different perl binaries (for example, when talking to a |
569 | different perl binaries (for example, when talking to a |
540 | process created with AnyEvent::Fork::Remote). |
570 | process created with AnyEvent::Fork::Remote). |
541 | |
571 | |
… | |
… | |
613 | Asynchronous Backend |
643 | Asynchronous Backend |
614 | For the asynchronous backend, things are more complicated: Whenever |
644 | For the asynchronous backend, things are more complicated: Whenever |
615 | it listens for another request by the parent, it might detect that |
645 | it listens for another request by the parent, it might detect that |
616 | the socket was closed (e.g. because the parent exited). It will sotp |
646 | the socket was closed (e.g. because the parent exited). It will sotp |
617 | listening for new requests and instead try to write out any |
647 | listening for new requests and instead try to write out any |
618 | remaining data (if any) or simply check whether the socket cna be |
648 | remaining data (if any) or simply check whether the socket can be |
619 | written to. After this, the RPC process is effectively done - no new |
649 | written to. After this, the RPC process is effectively done - no new |
620 | requests are incoming, no outstanding request data can be written |
650 | requests are incoming, no outstanding request data can be written |
621 | back. |
651 | back. |
622 | |
652 | |
623 | Since chances are high that there are event watchers that the RPC |
653 | Since chances are high that there are event watchers that the RPC |
… | |
… | |
688 | 1000 jobs are queued and the jobs are slow, they will all run |
718 | 1000 jobs are queued and the jobs are slow, they will all run |
689 | concurrently. The child must implement some queueing/limiting |
719 | concurrently. The child must implement some queueing/limiting |
690 | mechanism if this causes problems. Alternatively, the parent could |
720 | mechanism if this causes problems. Alternatively, the parent could |
691 | limit the amount of rpc calls that are outstanding. |
721 | limit the amount of rpc calls that are outstanding. |
692 | |
722 | |
693 | Blocking use of condvars is not supported. |
723 | Blocking use of condvars is not supported (in the main thread, |
|
|
724 | outside of e.g. Coro threads). |
694 | |
725 | |
695 | Using event-based modules such as IO::AIO, Gtk2, Tk and so on is |
726 | Using event-based modules such as IO::AIO, Gtk2, Tk and so on is |
696 | easy. |
727 | easy. |
697 | |
728 | |
698 | Passing file descriptors |
729 | Passing file descriptors |
… | |
… | |
760 | so you might want to look into AnyEvent::FDpasser which can handle the |
791 | so you might want to look into AnyEvent::FDpasser which can handle the |
761 | gory details. |
792 | gory details. |
762 | |
793 | |
763 | EXCEPTIONS |
794 | EXCEPTIONS |
764 | There are no provisions whatsoever for catching exceptions at this time |
795 | There are no provisions whatsoever for catching exceptions at this time |
765 | - in the child, exeptions might kill the process, causing calls to be |
796 | - in the child, exceptions might kill the process, causing calls to be |
766 | lost and the parent encountering a fatal error. In the parent, |
797 | lost and the parent encountering a fatal error. In the parent, |
767 | exceptions in the result callback will not be caught and cause undefined |
798 | exceptions in the result callback will not be caught and cause undefined |
768 | behaviour. |
799 | behaviour. |
769 | |
800 | |
770 | SEE ALSO |
801 | SEE ALSO |