Re: How to get Apache to use simultaneous connections to my fcgi app?

Sebastian Böhm <[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel,gmane.spam.detected
Message-ID <[email protected]>
Am 21.08.2007 um 03:28 schrieb joe-QzMH92Wc/[email protected]:

> On Aug 20, 2007, at 21:08 UTC, Bo Lindbergh wrote:
>
>> You may find `grep -w FCGI_GET_VALUES mod_fastcgi-2.4.2/*.c`
>> illuminating.
>>
>> In short, the only way to handle multiple requests simultaneously
>> with mod_fastcgi is to have it start multiple instances of your
>> application.
>
> Doh!  And in external mode, it doesn't start any instances of your
> application.  I'm using external mode because it's the only way I can
> find to make mod_fastcgi to use TCP sockets, rather than Unix sockets
> (which I don't have easy access to in my environment).

this is correct: you can use tcp only with external mode (this is a  
mod_fastcgi limitation, rather than a limitation of the fastcgi spec)


>
> So, is it the case that mod_fastcgi can only make multiple requests
> simultaneously for local apps communicating over Unix sockets?
>
> If so, that seems like a pretty severe limitation -- even if you app
> speaks Unix sockets, what if you want to run it on another machine for
> load-balancing?  If it can only get one request at a time, that's  
> going
> to wipe out most of the benefit of the external server, isn't it?


I modified the script to act as an external server and it worked.
This is how i configured it in apache : "FastCgiExternalServer /opt/ 
apache/htdocs/test123.fcpl -host 10.211.55.4:8888 -flush"
I did run the script on a differed machine. The "-flush" might be  
important depending on what you actually do.

Although I run only one instance of the script I could verify that it  
processed multiple requests at the same time.
The response this script created takes 10 seconds, printing one line  
every second, it could see the response line-by-line in two browser  
windows.

#!/usr/bin/perl

exit() if fork();
close stdin;
close stdout;
close stderr;

use strict;
use FCGI;
use Fcntl;
use POSIX qw(:errno_h);
use IO::Handle;

my $socker = FCGI::Opensocket(":8888",15);


my @fcgihandles = ();

for(1..10)
{
     push @fcgihandles,create_fcgi_handle($_)
}


while(1)
{

     # wait one second for new request
     my $rbits = '';
     vec( $rbits, 0 , 1 ) = 1; #fcgi socket has fileno 0
     my $nfound = select ($rbits, undef, undef, 1);

     # if there is a new connection accept it
     if($nfound >0)
     {
         foreach my $handle (@fcgihandles)
         {
             next if $handle->{'status'} ne 'available';

             if($handle->{'req'}->Accept() >= 0)
             {
                 my $out = $handle->{'out'};
                 print $out "Content-type: text/html\r\n\r 
\n<html><body>BEGIN ".$handle->{'id'}."<br/>\r\n";
                 $handle->{'req'}->Flush();
                 $handle->{'counter'}=1;
                 $handle->{'status'}='accepted';
                 last;
             }
             else
             {
                 warn 'under which circumstances does this happen?';
             }
         }
     }


     foreach my $handle (@fcgihandles)
     {
         next if $handle->{'status'} ne 'accepted';

         my $out = $handle->{'out'};

         print $out "line:".$handle->{'counter'}."<br/>\r\n";
         $handle->{'req'}->Flush();
         $handle->{'counter'}++;
         if($handle->{'counter'}==10)
         {
             print $out "finished </body></html>\r\n";
             $handle->{'req'}->Flush();
             $handle->{'req'}->Finish();
             $handle->{'status'}='available';
         }
     }
}



sub create_fcgi_handle($)
{
     my $id = shift;

     my %data = ();

     my %env = ();
     my $in = new IO::Handle;
     my $out = new IO::Handle;
     my $err = new IO::Handle;

     my $req = FCGI::Request($in,$out,$err, \%env,$socket);

     $in->autoflush(1);
     $out->autoflush(1);
     $out->autoflush(1);

     $data{'id'} = $id;
     $data{'in'} = $in;
     $data{'out'} = $out;
     $data{'err'} = $err;
     $data{'env'} = \%env;
     $data{'req'} = $req;
     $data{'status'} = 'available';

     return \%data;

}


>
> Thanks,
> - Joe
>
> --
> Joe Strout -- joe-QzMH92Wc/[email protected]
> Strout Custom Solutions, LLC
>
> ___________________________________
> fastcgi-developers mailing list
> http://fastcgi.com/fastcgi-developers/
>

___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.