… | |
… | |
450 | use AnyEvent; |
450 | use AnyEvent; |
451 | use AnyEvent::Util (); |
451 | use AnyEvent::Util (); |
452 | |
452 | |
453 | use IO::FDPass; |
453 | use IO::FDPass; |
454 | |
454 | |
455 | our $VERSION = '1.0'; |
455 | our $VERSION = 1.1; |
456 | |
456 | |
457 | # the early fork template process |
457 | # the early fork template process |
458 | our $EARLY; |
458 | our $EARLY; |
459 | |
459 | |
460 | # the empty template process |
460 | # the empty template process |
… | |
… | |
605 | |
605 | |
606 | You should use C<new> whenever possible, except when having a template |
606 | You should use C<new> whenever possible, except when having a template |
607 | process around is unacceptable. |
607 | process around is unacceptable. |
608 | |
608 | |
609 | The path to the perl interpreter is divined using various methods - first |
609 | The path to the perl interpreter is divined using various methods - first |
610 | C<$^X> is investigated to see if the path ends with something that sounds |
610 | C<$^X> is investigated to see if the path ends with something that looks |
611 | as if it were the perl interpreter. Failing this, the module falls back to |
611 | as if it were the perl interpreter. Failing this, the module falls back to |
612 | using C<$Config::Config{perlpath}>. |
612 | using C<$Config::Config{perlpath}>. |
613 | |
613 | |
|
|
614 | The path to perl can also be overriden by setting the global variable |
|
|
615 | C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent |
|
|
616 | invocations. |
|
|
617 | |
614 | =cut |
618 | =cut |
|
|
619 | |
|
|
620 | our $PERL; |
615 | |
621 | |
616 | sub new_exec { |
622 | sub new_exec { |
617 | my ($self) = @_; |
623 | my ($self) = @_; |
618 | |
624 | |
619 | return $EARLY->fork |
625 | return $EARLY->fork |
620 | if $EARLY; |
626 | if $EARLY; |
621 | |
627 | |
|
|
628 | unless (defined $PERL) { |
622 | # first find path of perl |
629 | # first find path of perl |
623 | my $perl = $; |
630 | my $perl = $; |
624 | |
631 | |
625 | # first we try $^X, but the path must be absolute (always on win32), and end in sth. |
632 | # first we try $^X, but the path must be absolute (always on win32), and end in sth. |
626 | # that looks like perl. this obviously only works for posix and win32 |
633 | # that looks like perl. this obviously only works for posix and win32 |
627 | unless ( |
634 | unless ( |
628 | ($^O eq "MSWin32" || $perl =~ m%^/%) |
635 | ($^O eq "MSWin32" || $perl =~ m%^/%) |
629 | && $perl =~ m%[/\\]perl(?:[0-9]+(\.[0-9]+)+)?(\.exe)?$%i |
636 | && $perl =~ m%[/\\]perl(?:[0-9]+(\.[0-9]+)+)?(\.exe)?$%i |
630 | ) { |
637 | ) { |
631 | # if it doesn't look perlish enough, try Config |
638 | # if it doesn't look perlish enough, try Config |
632 | require Config; |
639 | require Config; |
633 | $perl = $Config::Config{perlpath}; |
640 | $perl = $Config::Config{perlpath}; |
634 | $perl =~ s/(?:\Q$Config::Config{_exe}\E)?$/$Config::Config{_exe}/; |
641 | $perl =~ s/(?:\Q$Config::Config{_exe}\E)?$/$Config::Config{_exe}/; |
|
|
642 | } |
|
|
643 | |
|
|
644 | $PERL = $perl; |
635 | } |
645 | } |
636 | |
646 | |
637 | require Proc::FastSpawn; |
647 | require Proc::FastSpawn; |
638 | |
648 | |
639 | my ($fh, $slave) = AnyEvent::Util::portable_socketpair; |
649 | my ($fh, $slave) = AnyEvent::Util::portable_socketpair; |
… | |
… | |
647 | #local $ENV{PERL5LIB} = join ":", grep !ref, @INC; |
657 | #local $ENV{PERL5LIB} = join ":", grep !ref, @INC; |
648 | my %env = %ENV; |
658 | my %env = %ENV; |
649 | $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; |
659 | $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; |
650 | |
660 | |
651 | my $pid = Proc::FastSpawn::spawn ( |
661 | my $pid = Proc::FastSpawn::spawn ( |
652 | $perl, |
662 | $PERL, |
653 | ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], |
663 | ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], |
654 | [map "$_=$env{$_}", keys %env], |
664 | [map "$_=$env{$_}", keys %env], |
655 | ) or die "unable to spawn AnyEvent::Fork server: $!"; |
665 | ) or die "unable to spawn AnyEvent::Fork server: $!"; |
656 | |
666 | |
657 | $self->_new ($fh, $pid) |
667 | $self->_new ($fh, $pid) |
… | |
… | |
847 | |
857 | |
848 | $self->[CB] = $cb; |
858 | $self->[CB] = $cb; |
849 | $self->_cmd (r => $func); |
859 | $self->_cmd (r => $func); |
850 | } |
860 | } |
851 | |
861 | |
|
|
862 | =back |
|
|
863 | |
|
|
864 | =head2 EXPERIMENTAL METHODS |
|
|
865 | |
|
|
866 | These methods might go away completely or change behaviour, at any time. |
|
|
867 | |
|
|
868 | =over 4 |
|
|
869 | |
852 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
870 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
853 | |
871 | |
854 | Flushes all commands out to the process and then calls the callback with |
872 | Flushes all commands out to the process and then calls the callback with |
855 | the communications socket. |
873 | the communications socket. |
856 | |
874 | |
… | |
… | |
1043 | (part of this distribution). |
1061 | (part of this distribution). |
1044 | |
1062 | |
1045 | L<AnyEvent::Fork::Template>, to create a process by forking the main |
1063 | L<AnyEvent::Fork::Template>, to create a process by forking the main |
1046 | program at a convenient time (part of this distribution). |
1064 | program at a convenient time (part of this distribution). |
1047 | |
1065 | |
|
|
1066 | L<AnyEvent::Fork::Remote>, for another way to create processes that is |
|
|
1067 | mostly compatible to this module and modules building on top of it, but |
|
|
1068 | works better with remote processes. |
|
|
1069 | |
1048 | L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). |
1070 | L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). |
1049 | |
1071 | |
1050 | L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN). |
1072 | L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN). |
1051 | |
1073 | |
1052 | =head1 AUTHOR AND CONTACT INFORMATION |
1074 | =head1 AUTHOR AND CONTACT INFORMATION |