Re: fastcgi with perl, php, etc
Sam Vilain <sam-s9IUA/[email protected]>
| Newsgroups | gmane.comp.web.fastcgi.devel |
|---|---|
| Message-ID | <[email protected]> |
fork() is not the expensive part, relatively speaking. For instance,
the performance of the cgi-fcgi wrapper is still very good! exec() on a
large binary like Perl, and compiling the script and/or libraries every
request is what really hurts. So, the solution I gave eliminated
exec(), but not compilation.
PHP gets around this problem by caching compiled pages (via Zend).
The reason for the fork(), is directly afterwards root permissions are
dropped. The reason for losing the FCGI::Request object in the parent
process is that it is only relevant to the child that is processing the
request.
I considered this problem for a while, and came to no satisfactory
answer that didn't intrude on legacy CGI applications, or involve the
overhead of page compiling for every request.
You could use SUExec, and set handlers to fcgi-script instead of
cgi-script, so that all CGI's are run as FCGI on the system. This would
mean that *every CGI script* would become a persistent process, managed
by mod_fastcgi (or the FastCGI process manager of whatever web server
you're using on the front end). You'd need to make a wrapper `perl'
(that all CGI scripts must use in their #! lines, but that is
comparatively acceptable) that handles loading the FCGI libraries,
keeping the process persistent, etc.
An alternative would to simply go the normal "who needs UNIX security,
anyway?" CGI approach, and simply keep a pool of processes all running
as the same usercode, but again to avoid users stepping on each other's
toes, you need to be able to throw away the interpreter state at the end
of every request. Probably the easiest way to achieve this is to use
another Apache instance with mod_perl compiled into it, and use
mod_rewrite + mod_proxy in the front end server to redirect requests to
it. It's a pity that Apache can't support being a FastCGI client.
HTH,
Sam.
Gustavo A. Baratto wrote:
>Thanks for reply....
>
>Do I really have to fork?
>What would be the drawbacks if I just use 'require' function to execute the
>user script?
>-----
>#!/usr/local/bin/perl
>
>use FCGI;
>
>my $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV);
>my $count = 0;
>while( $request->Accept() >= 0 ) {
> do($ENV{PATH_TRANSLATED});
>}
>----
>
>I want to avoid forking, as I wouldn't have much speed gain over normal cgi
>scripts.
>
>Another question... why do I have to throw away the old handle?
> if (fork()) {
> $request = FCGI::Request(...);
> else
>
>
>----- Original Message -----
>From: "Sam Vilain" <sam-s9IUA/[email protected]>
>To: "Gustavo A. Baratto" <[email protected]>
>Cc: <[email protected]>
>Sent: Thursday, August 19, 2004 3:47 PM
>Subject: Re: [FASTCGI] fastcgi with perl, php, etc
>
>
>
>
>>Gustavo A. Baratto wrote:
>>
>>
>>
>>>My problem is perl though... Basically, I want to do the same thing I did
>>>php to the other scripting languages. I want to pre-fork a few perl,
>>>
>>>
>python
>
>
>>>or tcl processes, and redirect the cgi requests to these pre-forked
>>>processes to improve speed.
>>>All the examples and docs I have seen over the net dont treat the script
>>>defined by FastCgiServer as a general 'perl server' as it is for php. I
>>>
>>>
>need
>
>
>>>'FastCgiServer /usr/bin/perl' to be the pre-forked interpreter for all
>>>
>>>
>.pl
>
>
>>>files.
>>>
>>>
>>>
>>>
>>You'll need to write a wrapper in Perl (that runs as root), something
>>along the lines of;
>>
>>use FCGI;
>>my $socket = FCGI::OpenSocket(...);
>>my $request = FCGI::Request(...);
>>while ($request->accept()) {
>> ($path, $script) = ($ENV{SCRIPT_NAME} =~ m{.*?/([^/]*)$});
>>
>> if (fork()) {
>> # throw away the old handle
>> $request = FCGI::Request(...);
>> } else {
>> chdir($path);
>> $( = $) = (stat ".")[5];
>> $< = $> = (stat _)[4];
>> require './$script';
>> exit(0);
>> }
>>}
>>
>>You'll need to experiment with the best way to handle the forking for
>>your application.
>>
>>--
>>Sam Vilain, sam /\T vilain |><>T net, PGP key ID: 0x05B52F13
>>(include my PGP key ID in personal replies to avoid spam filtering)
>>
>>
>>
>
>
>
--
Sam Vilain, sam /\T vilain |><>T net, PGP key ID: 0x05B52F13
(include my PGP key ID in personal replies to avoid spam filtering)
___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/