开发者

Is FastCgi thread-safe?

I'm a little confused about how FastCGI works. Is there really only one instance of my program running or there some magic threading going on? This is important because if I have data structures that have scope outside the m开发者_如何转开发ain loop, I need to know if these data structures need to be thread-safe.

EDIT: My app is in Perl and here is my apache2 config:

<IfModule mod_fastcgi.c>
   AddHandler fastcgi-script .fcgi .fcg
   FastCgiServer /usr/lib/app/process.fcg -idle-timeout 60 -processes 1
 </IfModule>

Just to be clear what I'm asking... for this code:

use CGI::Fast qw/:standard/;

my %sharedHash;

while (new CGI::Fast) {
     # do stuff with %sharedHash
}

Is the "do stuff" part safe or is some "multi-threading magic" going on that might mean that more than one thread is executing "do stuff" at the same time, thus corrupting %sharedHash?


FastCGI itself is just an interface between your web server and your app. Your app can be multithreaded (almost always the case with Java, often in Python), or written in asynchronous, event-driven style (Twisted in Python, Node.js, etc). If the former, then you need to make sure that your access to the global state structures are properly thread-synchronized.

From the FastCGI whitepaper: Architecture independence. CGI is not tied to any particular server architecture (single threaded, multi-threaded, etc.).


I believe that it depends on the Fast-CGI application. Looking at the FastCGI specification section 4 (Management Record Types) the application specifies 3 values regarding concurrent connections:

  • FCGI_MAX_CONNS: The maximum number of concurrent transport connections this application will accept, e.g. "1" or "10".

  • FCGI_MAX_REQS: The maximum number of concurrent requests this application will accept, e.g. "1" or "50".

  • FCGI_MPXS_CONNS: "0" if this application does not multiplex connections (i.e. handle concurrent requests over each connection), "1" otherwise.

From this it appears that as long as your application specifies 1 for FCGI_MAX_CONNS and FCGI_MAX_REQS, and 0 for FCGI_MPXS_CONNS then any FastCTI compliant web server will treat your application as entirely single threaded, presumably starting new processes to handle concurrent requests to the web server.

Update: Regarding "magic threading" and your data structures, unless you share your data structures between multiple requests I can't see any reason why they would need to be thread safe - I can't see any reason why multiple threads would be used to handle a single request, however this does depend on your FastCGI library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜