… | |
… | |
591 | |
591 | |
592 | If you want to execute some code (that isn't in a module) to take over the |
592 | If you want to execute some code (that isn't in a module) to take over the |
593 | process, you should compile a function via C<eval> first, and then call |
593 | process, you should compile a function via C<eval> first, and then call |
594 | it via C<run>. This also gives you access to any arguments passed via the |
594 | it via C<run>. This also gives you access to any arguments passed via the |
595 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
595 | C<send_xxx> methods, such as file handles. See the L<use AnyEvent::Fork as |
596 | a faster fork+exec> example. |
596 | a faster fork+exec> example to see it in action. |
597 | |
597 | |
598 | Returns the process object for easy chaining of method calls. |
598 | Returns the process object for easy chaining of method calls. |
599 | |
599 | |
600 | =cut |
600 | =cut |
601 | |
601 | |
… | |
… | |
627 | =item $proc = $proc->send_fh ($handle, ...) |
627 | =item $proc = $proc->send_fh ($handle, ...) |
628 | |
628 | |
629 | Send one or more file handles (I<not> file descriptors) to the process, |
629 | Send one or more file handles (I<not> file descriptors) to the process, |
630 | to prepare a call to C<run>. |
630 | to prepare a call to C<run>. |
631 | |
631 | |
632 | The process object keeps a reference to the handles until this is done, |
632 | The process object keeps a reference to the handles until they have |
633 | so you must not explicitly close the handles. This is most easily |
633 | been passed over to the process, so you must not explicitly close the |
634 | accomplished by simply not storing the file handles anywhere after passing |
634 | handles. This is most easily accomplished by simply not storing the file |
635 | them to this method. |
635 | handles anywhere after passing them to this method - when AnyEvent::Fork |
|
|
636 | is finished using them, perl will automatically close them. |
636 | |
637 | |
637 | Returns the process object for easy chaining of method calls. |
638 | Returns the process object for easy chaining of method calls. |
638 | |
639 | |
639 | Example: pass a file handle to a process, and release it without |
640 | Example: pass a file handle to a process, and release it without |
640 | closing. It will be closed automatically when it is no longer used. |
641 | closing. It will be closed automatically when it is no longer used. |
… | |
… | |
656 | } |
657 | } |
657 | |
658 | |
658 | =item $proc = $proc->send_arg ($string, ...) |
659 | =item $proc = $proc->send_arg ($string, ...) |
659 | |
660 | |
660 | Send one or more argument strings to the process, to prepare a call to |
661 | Send one or more argument strings to the process, to prepare a call to |
661 | C<run>. The strings can be any octet string. |
662 | C<run>. The strings can be any octet strings. |
662 | |
663 | |
663 | The protocol is optimised to pass a moderate number of relatively short |
664 | The protocol is optimised to pass a moderate number of relatively short |
664 | strings - while you can pass up to 4GB of data in one go, this is more |
665 | strings - while you can pass up to 4GB of data in one go, this is more |
665 | meant to pass some ID information or other startup info, not big chunks of |
666 | meant to pass some ID information or other startup info, not big chunks of |
666 | data. |
667 | data. |
… | |
… | |
682 | Enter the function specified by the function name in C<$func> in the |
683 | Enter the function specified by the function name in C<$func> in the |
683 | process. The function is called with the communication socket as first |
684 | process. The function is called with the communication socket as first |
684 | argument, followed by all file handles and string arguments sent earlier |
685 | argument, followed by all file handles and string arguments sent earlier |
685 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
686 | via C<send_fh> and C<send_arg> methods, in the order they were called. |
686 | |
687 | |
|
|
688 | The process object becomes unusable on return from this function - any |
|
|
689 | further method calls result in undefined behaviour. |
|
|
690 | |
687 | The function name should be fully qualified, but if it isn't, it will be |
691 | The function name should be fully qualified, but if it isn't, it will be |
688 | looked up in the main package. |
692 | looked up in the C<main> package. |
689 | |
693 | |
690 | If the called function returns, doesn't exist, or any error occurs, the |
694 | If the called function returns, doesn't exist, or any error occurs, the |
691 | process exits. |
695 | process exits. |
692 | |
696 | |
693 | Preparing the process is done in the background - when all commands have |
697 | Preparing the process is done in the background - when all commands have |
694 | been sent, the callback is invoked with the local communications socket |
698 | been sent, the callback is invoked with the local communications socket |
695 | as argument. At this point you can start using the socket in any way you |
699 | as argument. At this point you can start using the socket in any way you |
696 | like. |
700 | like. |
697 | |
|
|
698 | The process object becomes unusable on return from this function - any |
|
|
699 | further method calls result in undefined behaviour. |
|
|
700 | |
701 | |
701 | If the communication socket isn't used, it should be closed on both sides, |
702 | If the communication socket isn't used, it should be closed on both sides, |
702 | to save on kernel memory. |
703 | to save on kernel memory. |
703 | |
704 | |
704 | The socket is non-blocking in the parent, and blocking in the newly |
705 | The socket is non-blocking in the parent, and blocking in the newly |
… | |
… | |
779 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
780 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
780 | |
781 | |
781 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
782 | So how can C<< AnyEvent->new >> be faster than a standard fork, even |
782 | though it uses the same operations, but adds a lot of overhead? |
783 | though it uses the same operations, but adds a lot of overhead? |
783 | |
784 | |
784 | The difference is simply the process size: forking the 6MB process takes |
785 | The difference is simply the process size: forking the 5MB process takes |
785 | so much longer than forking the 2.5MB template process that the overhead |
786 | so much longer than forking the 2.5MB template process that the extra |
786 | introduced is canceled out. |
787 | overhead introduced is canceled out. |
787 | |
788 | |
788 | If the benchmark process grows, the normal fork becomes even slower: |
789 | If the benchmark process grows, the normal fork becomes even slower: |
789 | |
790 | |
790 | 1340 new processes, manual fork in a 20MB process |
791 | 1340 new processes, manual fork of a 20MB process |
791 | 731 new processes, manual fork in a 200MB process |
792 | 731 new processes, manual fork of a 200MB process |
792 | 235 new processes, manual fork in a 2000MB process |
793 | 235 new processes, manual fork of a 2000MB process |
793 | |
794 | |
794 | What that means (to me) is that I can use this module without having a |
795 | What that means (to me) is that I can use this module without having a bad |
795 | very bad conscience because of the extra overhead required to start new |
796 | conscience because of the extra overhead required to start new processes. |
796 | processes. |
|
|
797 | |
797 | |
798 | =head1 TYPICAL PROBLEMS |
798 | =head1 TYPICAL PROBLEMS |
799 | |
799 | |
800 | This section lists typical problems that remain. I hope by recognising |
800 | This section lists typical problems that remain. I hope by recognising |
801 | them, most can be avoided. |
801 | them, most can be avoided. |
802 | |
802 | |
803 | =over 4 |
803 | =over 4 |
804 | |
804 | |
805 | =item "leaked" file descriptors for exec'ed processes |
805 | =item leaked file descriptors for exec'ed processes |
806 | |
806 | |
807 | POSIX systems inherit file descriptors by default when exec'ing a new |
807 | POSIX systems inherit file descriptors by default when exec'ing a new |
808 | process. While perl itself laudably sets the close-on-exec flags on new |
808 | process. While perl itself laudably sets the close-on-exec flags on new |
809 | file handles, most C libraries don't care, and even if all cared, it's |
809 | file handles, most C libraries don't care, and even if all cared, it's |
810 | often not possible to set the flag in a race-free manner. |
810 | often not possible to set the flag in a race-free manner. |
… | |
… | |
830 | libraries or the code that leaks those file descriptors. |
830 | libraries or the code that leaks those file descriptors. |
831 | |
831 | |
832 | Fortunately, most of these leaked descriptors do no harm, other than |
832 | Fortunately, most of these leaked descriptors do no harm, other than |
833 | sitting on some resources. |
833 | sitting on some resources. |
834 | |
834 | |
835 | =item "leaked" file descriptors for fork'ed processes |
835 | =item leaked file descriptors for fork'ed processes |
836 | |
836 | |
837 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
837 | Normally, L<AnyEvent::Fork> does start new processes by exec'ing them, |
838 | which closes file descriptors not marked for being inherited. |
838 | which closes file descriptors not marked for being inherited. |
839 | |
839 | |
840 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
840 | However, L<AnyEvent::Fork::Early> and L<AnyEvent::Fork::Template> offer |
… | |
… | |
849 | |
849 | |
850 | The solution is to either not load these modules before use'ing |
850 | The solution is to either not load these modules before use'ing |
851 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay |
851 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template>, or to delay |
852 | initialising them, for example, by calling C<init Gtk2> manually. |
852 | initialising them, for example, by calling C<init Gtk2> manually. |
853 | |
853 | |
854 | =item exit runs destructors |
854 | =item exiting calls object destructors |
855 | |
855 | |
856 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
856 | This only applies to users of Lc<AnyEvent::Fork:Early> and |
857 | L<AnyEvent::Fork::Template>. |
857 | L<AnyEvent::Fork::Template>. |
858 | |
858 | |
859 | When a process created by AnyEvent::Fork exits, it might do so by calling |
859 | When a process created by AnyEvent::Fork exits, it might do so by calling |
… | |
… | |
881 | to make it so, mostly due to the bloody broken perl that nobody seems to |
881 | to make it so, mostly due to the bloody broken perl that nobody seems to |
882 | care about. The fork emulation is a bad joke - I have yet to see something |
882 | care about. The fork emulation is a bad joke - I have yet to see something |
883 | useful that you can do with it without running into memory corruption |
883 | useful that you can do with it without running into memory corruption |
884 | issues or other braindamage. Hrrrr. |
884 | issues or other braindamage. Hrrrr. |
885 | |
885 | |
886 | Cygwin perl is not supported at the moment, as it should implement fd |
886 | Cygwin perl is not supported at the moment due to some hilarious |
887 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
887 | shortcomings of its API - see L<IO::FDPoll> for more details. |
888 | support enough functionality to do it. |
|
|
889 | |
888 | |
890 | =head1 SEE ALSO |
889 | =head1 SEE ALSO |
891 | |
890 | |
892 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
891 | L<AnyEvent::Fork::Early> (to avoid executing a perl interpreter), |
893 | L<AnyEvent::Fork::Template> (to create a process by forking the main |
892 | L<AnyEvent::Fork::Template> (to create a process by forking the main |