Re: FCGI process handling and fine-tunning for saving CPU, under Perl code
Chris Lightfoot <[email protected]>
| Newsgroups | gmane.comp.web.fastcgi.devel |
|---|---|
| Message-ID | <[email protected]> |
On Fri, Dec 02, 2005 at 02:18:55PM -0200, phoenix_fly wrote:
>
> Here´s the problem: I´ve finally made FCGI (and also
> CGI::Fast, but not mod_perl, as my host doesn´t support
> it) work at my shared account. But just can´t figure out
> how to handle it, I mean, how do I restart the processes
> when I update the script code? How do I know the number of
> the process??
Two options: you can have the mod_fastcgi process manager
(if you use that) notice when your script file is updated
on disk and then automatically restart it when it is
(assuming you can switch that option on); this is limited
by the fact that it only checks your main script file, so
if you have modules which you also change, this won't help
you. Alternatively, you can check the timestamp on your
script files in your main loop. We have code to do this
here:
https://secure.mysociety.org/cvstrac/getfile/mysociety/perllib/mySociety/WatchUpdate.pm
though it's a little ugly.
> My main goal with FCGI is to save precious CPU minutes,
> as the traffic is increasing a lot (and my host will kick
> me out if I get too heavy-processing, as they just dont
> have dedicated servers available). You guys know if the
> CPU savings are worth adapting the code? I mean, can I
> expect to use, like, 30% less CPU?
That depends on the startup cost of your program vs the
marginal cost of serving one request, basically. Without
knowing any more detail it's impossible to comment; really
you need to measure it now and decide whether it's
worthwhile.
Note that with FastCGI you can hold resources like
database handles, caches etc. in between requests, so you
may be able to make savings by altering the structure of
your program a bit.
> And, last but not least, I am not quite sure about
> converting guidelines, like avoiding referencing $q (new
> CGI) inside subs and all. If you can offer me some links
> for that, it would be great too.
Again, without seeing your code, it's impossible to
comment, but the basic skeleton I'd recommend for a
perl FastCGI server would be:
use CGI;
use CGI::Fast;
while (my $q = new CGI::Fast()) {
# do something with $q to serve the request
do_something_in_a_subroutine($q);
...
}
-- in particular, avoid using any global methods from the
CGI object. That way you can make sure that you aren't
going to accidentally carry over state from one request to
another, and will also pay attention more to the code
you're writing :-)
--
``Some people, when confronted with a problem, think,
`I know, I'll use regular expressions.'
Now they have two problems.'' (Jamie Zawinski)
___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/