[Twisted-Python] How can I change max simultaneously connection in twisted

孙绍轩 sunshaoxuan at cdeledu.com
Tue May 5 07:33:57 EDT 2009

Hello all

I just wrote a small code to holding many http requests until server's
status change. Everything is fine but when simultaneously connection up
to 1k+, the server code didn't response to new connection. I've set
ulimit -n's value to 65535 and the code using epollreactor mode. Are
there have some limitations? How can I configure to support more
BTW: I use Python 2.4.3 on CentOS 5 64-bit. follow is the code, Thanks
for you help!

from twisted.internet import epollreactor

from twisted.internet import reactor, defer, threads
from twisted.web import server, resource, client
from twisted.python import log
from time import sleep
import sys

def checkstatus_wait(laststatus):
timeout = 25
while timeout > 0:
if int(laststatus) < status:
return True
timeout -= 1
return False

class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
if "cmd" in request.args:
cmd = request.args['cmd'][0]
cmd = ""

if cmp(cmd,"listen") == 0:
if "laststatus" in request.args:
last = request.args['laststatus'][0]
if int(last) < status:
return "ret=1\nerrstring=need update\nlaststatus=%d\n\n" % status
d = threads.deferToThread(checkstatus_wait, last)
d.addCallback(self.testReturn, request)
return server.NOT_DONE_YET
return "ret=1\nerrstring=need update\nlaststatus=%d\n\n" % status
elif cmp(cmd,"updatestatus") == 0:
if status < 1000000:
status += 1
status = 0
return "ret=1\nerrstring=success update\n\n"
return "ret=-1\nerrstring=wrong cmd\n\n"

def testReturn(self, ret, request):
if ret:
request.write("ret=1\nerrstring=need update\nlaststatus=%d\n\n" % status)
request.write("ret=0\nerrstring=no need update\n\n")

status = 0

site = server.Site(Simple())

reactor.listenTCP(8080, site)


孙绍轩 Yorgo Sun
China Distance Education Holdings Limited
18th Floor, XueYuan International Tower, No.1 ZhiChun Road, HaiDian District, Beijing, China 100083
Phone: +8610-8231-9999 Ext 708
This message is intended only for the use of the individual or entity to whom it is addressed and contains information that is privileged and confidential. If you, the reader of this message, are not the intended recipient, you should not disseminate, distribute or copy this communication. If you have received this communication in error, please notify us immediately by return email and delete the original message.

More information about the Twisted-Python mailing list