ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork/Fork.pm
(Generate patch)

Comparing AnyEvent-Fork/Fork.pm (file contents):
Revision 1.46 by root, Thu Apr 18 11:18:23 2013 UTC vs.
Revision 1.50 by root, Sat Apr 20 19:58:06 2013 UTC

377use AnyEvent; 377use AnyEvent;
378use AnyEvent::Util (); 378use AnyEvent::Util ();
379 379
380use IO::FDPass; 380use IO::FDPass;
381 381
382our $VERSION = 0.6; 382our $VERSION = 0.7;
383 383
384# the early fork template process 384# the early fork template process
385our $EARLY; 385our $EARLY;
386 386
387# the empty template process 387# the empty template process
435 # send string 435 # send string
436 my $len = syswrite $self->[FH], $self->[QUEUE][0]; 436 my $len = syswrite $self->[FH], $self->[QUEUE][0];
437 437
438 unless ($len) { 438 unless ($len) {
439 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK; 439 return if $! == Errno::EAGAIN || $! == Errno::EWOULDBLOCK;
440 undef $self->[3]; 440 undef $self->[WW];
441 die "AnyEvent::Fork: command write failure: $!"; 441 die "AnyEvent::Fork: command write failure: $!";
442 } 442 }
443 443
444 substr $self->[QUEUE][0], 0, $len, ""; 444 substr $self->[QUEUE][0], 0, $len, "";
445 shift @{ $self->[QUEUE] } unless length $self->[QUEUE][0]; 445 shift @{ $self->[QUEUE] } unless length $self->[QUEUE][0];
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)) # EXPERIMENTAL, MIGHT BE REMOVED
780
781Flushes all commands out to the process and then calls the callback with
782the communications socket.
783
784The process object becomes unusable on return from this function - any
785further method calls result in undefined behaviour.
786
787The point of this method is to give you a file handle thta you cna pass
788to another process. In that other process, you can call C<new_from_fh
789AnyEvent::Fork::RPC> to create a new C<AnyEvent::Fork> object from it,
790thereby effectively passing a fork object to another process.
791
792=cut
793
794sub 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 # EXPERIMENTAL, MIGHT BE REMOVED
806
807Takes a file handle originally rceeived by the C<to_fh> method and creates
808a new C<AnyEvent:Fork> object. The child process itself will not change in
809any way, i.e. it will keep all the modifications done to it before calling
810C<to_fh>.
811
812The new object is very much like the original object, except that the
813C<pid> method will return C<undef> even if the process is a direct child.
814
815=cut
816
817sub 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
780Now for some unscientific benchmark numbers (all done on an amd64 827Now 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
791Then I did the same thing, but instead of calling fork, I called 838Then I did the same thing, but instead of calling fork, I called
792AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the 839AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the
793socket form the child to close on exit. This does the same thing as manual 840socket from the child to close on exit. This does the same thing as manual
794socket pair + fork, except that what is forked is the template process 841socket 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
796of the socket first. 843of 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
905to make it so, mostly due to the bloody broken perl that nobody seems to 952to make it so, mostly due to the bloody broken perl that nobody seems to
906care about. The fork emulation is a bad joke - I have yet to see something 953care about. The fork emulation is a bad joke - I have yet to see something
907useful that you can do with it without running into memory corruption 954useful that you can do with it without running into memory corruption
908issues or other braindamage. Hrrrr. 955issues or other braindamage. Hrrrr.
909 956
957Since fork is endlessly broken on win32 perls (it doesn't even remotely
958work within it's documented limits) and quite obviously it's not getting
959improved any time soon, the best way to proceed on windows would be to
960always use C<new_exec> and thus never rely on perl's fork "emulation".
961
910Cygwin perl is not supported at the moment due to some hilarious 962Cygwin perl is not supported at the moment due to some hilarious
911shortcomings of its API - see L<IO::FDPoll> for more details. 963shortcomings of its API - see L<IO::FDPoll> for more details. If you never
964use C<send_fh> and always use C<new_exec> to create processes, it should
965work though.
912 966
913=head1 SEE ALSO 967=head1 SEE ALSO
914 968
915L<AnyEvent::Fork::Early>, to avoid executing a perl interpreter at all 969L<AnyEvent::Fork::Early>, to avoid executing a perl interpreter at all
916(part of this distribution). 970(part of this distribution).

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines