[Twisted-Python] twisted.web2 only can serve one connection at the same time when using scgi?

Chris chris.yan at saybot.com
Tue Jun 23 04:31:21 EDT 2009

Hi everyone,
I am trying to use apache2 + scgi to write a application,I found
twisted.web2 supports the scgi module.I began to use this library to
write some code to test how to use it,just like this:

from twisted.web2 import server, channel, resource,http,http_headers
from twisted.application import service, strports
from twisted.internet import reactor, defer

class Test(resource.Resource):
def getResponse(self, result = None):
rep = "The output which shall be transferred to the browser."
res = http.Response( 200,
{'content-type': http_headers.MimeType('text','plain')},
print "---- getResponse returning: ", res
return res

def render(self,request):
print 'a new request is coming'
d = defer.Deferred()
import time
d.addCallback(lambda x:x).addCallback(self.getResponse)
return d

toplevel = Test()
site = server.Site(toplevel)

application = service.Application("demoserver")
s = strports.service('tcp:8888', channel.SCGIFactory(site))

in the render function,I used a defer to simulate a time-consuming
operation which takes ten seconds.IF in twisted.web,I can first return a
server.NOT_DONE_YET,and use request.write() and request.finish() when
the time-consuming operation is done(still use defer to do this).but
twisted.web2 only supports returning a iweb.IResponse.it seems returning
a defer is the only way.
_|| <http://python.net/crew/mwh/apidocs/twisted.web2.iweb.IResponse.html>_
here is the output:
2009-06-23 15:45:11+0800 [-] Log opened.
2009-06-23 15:45:11+0800 [-] twistd 8.2.0 (/usr/bin/python 2.5.2)
starting up.
2009-06-23 15:45:11+0800 [-] reactor class:
2009-06-23 15:45:11+0800 [-] twisted.web2.channel.scgi.SCGIFactory
starting on 8888
2009-06-23 15:45:11+0800 [-] Starting factory
<twisted.web2.channel.scgi.SCGIFactory instance at 0xf213f8>
2009-06-23 15:45:26+0800 [SCGIChannelRequest,0,] a new
request is coming
2009-06-23 15:45:36+0800 [-] ---- getResponse returning:
<twisted.web2.http.Response code=200, streamlen=53>
2009-06-23 15:45:36+0800 [SCGIChannelRequest,1,] a new
request is coming
2009-06-23 15:45:46+0800 [-] ---- getResponse returning:
<twisted.web2.http.Response code=200, streamlen=53>
2009-06-23 15:45:46+0800 [SCGIChannelRequest,2,] a new
request is coming
2009-06-23 15:45:56+0800 [-] ---- getResponse returning:
<twisted.web2.http.Response code=200, streamlen=53>

I use the 3 browsers to access the URL within 1 second,but as we all see
in the log,the server accepts the requests one by one,that is really not
acceptable.I checked a little about the twisted.web2 source code, this
render function is in a defer's callbacks chain,when I return a defer in
a defer's callbacks chain,sure,the rest of the callbacks will wait for
my defer to finish ,even I run it with reactor.callLater.so far,I feel I
can do nothing about this,anyone can help me about this?


More information about the Twisted-Python mailing list