[Twisted-Python] How can I change max simultaneously connection in twisted
孙绍轩
sunshaoxuan at cdeledu.com
Tue May 5 05:33:57 MDT 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
connections?
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
epollreactor.install()
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
else:
timeout -= 1
sleep(1)
return False
class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
if "cmd" in request.args:
cmd = request.args['cmd'][0]
else:
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
else:
d = threads.deferToThread(checkstatus_wait, last)
d.addCallback(self.testReturn, request)
return server.NOT_DONE_YET
else:
return "ret=1\nerrstring=need update\nlaststatus=%d\n\n" % status
elif cmp(cmd,"updatestatus") == 0:
if status < 1000000:
status += 1
else:
status = 0
#client.getPage('http://211.157.0.47/videochat/listen?cmd=updatestatus')
return "ret=1\nerrstring=success update\n\n"
else:
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)
else:
request.write("ret=0\nerrstring=no need update\n\n")
request.finish()
status = 0
log.startLogging(sys.stdout)
site = server.Site(Simple())
reactor.suggestThreadPoolSize(5000)
reactor.listenTCP(8080, site)
reactor.run()
--
孙绍轩 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
***********************************************************
CONFIDENTIALITY CAUTION:
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