… | |
… | |
448 | |
448 | |
449 | # everything written |
449 | # everything written |
450 | undef $self->[WW]; |
450 | undef $self->[WW]; |
451 | |
451 | |
452 | # invoke run callback, if any |
452 | # invoke run callback, if any |
|
|
453 | if ($self->[CB]) { |
453 | $self->[CB]->($self->[FH]) if $self->[CB]; |
454 | $self->[CB]->($self->[FH]); |
|
|
455 | @$self = (); |
|
|
456 | } |
454 | }; |
457 | }; |
455 | |
458 | |
456 | () # make sure we don't leak the watcher |
459 | () # make sure we don't leak the watcher |
457 | } |
460 | } |
458 | |
461 | |
… | |
… | |
771 | |
774 | |
772 | $self->[CB] = $cb; |
775 | $self->[CB] = $cb; |
773 | $self->_cmd (r => $func); |
776 | $self->_cmd (r => $func); |
774 | } |
777 | } |
775 | |
778 | |
|
|
779 | =item $proc->to_fh ($cb->($fh)) |
|
|
780 | |
|
|
781 | Flushes all commands out to the process and then calls the callback with |
|
|
782 | the communications socket. |
|
|
783 | |
|
|
784 | The process object becomes unusable on return from this function - any |
|
|
785 | further method calls result in undefined behaviour. |
|
|
786 | |
|
|
787 | The point of this method is to give you a file handle thta you cna pass |
|
|
788 | to another process. In that other process, you can call C<new_from_fh |
|
|
789 | AnyEvent::Fork::RPC> to create a new C<AnyEvent::Fork> object from it, |
|
|
790 | thereby effectively passing a fork object to another process. |
|
|
791 | |
|
|
792 | =cut |
|
|
793 | |
|
|
794 | sub to_fh { |
|
|
795 | my ($self, $cb) = @_; |
|
|
796 | |
|
|
797 | $self->[CB] = $cb; |
|
|
798 | |
|
|
799 | unless ($self->[WW]) { |
|
|
800 | $self->[CB]->($self->[FH]); |
|
|
801 | @$self = (); |
|
|
802 | } |
|
|
803 | } |
|
|
804 | |
|
|
805 | =item new_from_fh AnyEvent::Fork $fh |
|
|
806 | |
|
|
807 | Takes a file handle originally rceeived by the C<to_fh> method and creates |
|
|
808 | a new C<AnyEvent:Fork> object. The child process itself will not change in |
|
|
809 | any way, i.e. it will keep all the modifications done to it before calling |
|
|
810 | C<to_fh>. |
|
|
811 | |
|
|
812 | The new object is very much like the original object, except that the |
|
|
813 | C<pid> method will return C<undef> even if the process is a direct child. |
|
|
814 | |
|
|
815 | =cut |
|
|
816 | |
|
|
817 | sub new_from_fh { |
|
|
818 | my ($class, $fh) = @_; |
|
|
819 | |
|
|
820 | $class->_new ($fh) |
|
|
821 | } |
|
|
822 | |
776 | =back |
823 | =back |
777 | |
824 | |
778 | =head1 PERFORMANCE |
825 | =head1 PERFORMANCE |
779 | |
826 | |
780 | Now for some unscientific benchmark numbers (all done on an amd64 |
827 | Now for some unscientific benchmark numbers (all done on an amd64 |
… | |
… | |
788 | |
835 | |
789 | 2079 new processes per second, using manual socketpair + fork |
836 | 2079 new processes per second, using manual socketpair + fork |
790 | |
837 | |
791 | Then I did the same thing, but instead of calling fork, I called |
838 | Then I did the same thing, but instead of calling fork, I called |
792 | AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
839 | AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
793 | socket form the child to close on exit. This does the same thing as manual |
840 | socket from the child to close on exit. This does the same thing as manual |
794 | socket pair + fork, except that what is forked is the template process |
841 | socket pair + fork, except that what is forked is the template process |
795 | (2440kB), and the socket needs to be passed to the server at the other end |
842 | (2440kB), and the socket needs to be passed to the server at the other end |
796 | of the socket first. |
843 | of the socket first. |
797 | |
844 | |
798 | 2307 new processes per second, using AnyEvent::Fork->new |
845 | 2307 new processes per second, using AnyEvent::Fork->new |