<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1476" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=901561010-16032005><FONT face=Arial
size=2>Hello.</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial size=2>I ran the (Twisted)
server code below and it accepts a client socket connection every 1.3
seconds. The equivalent Synchronous socket code accepts one every
2 seconds. </FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial size=2>Can anyone here
suggest any code improvements for my Twisted server to accept
connections faster ?</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial size=2>I've include the
Synchronous socket server code and the client generator code below as
well.</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>#
==========================================================================================</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>#
Twisted server code</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>#
==========================================================================================</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>from
twisted.protocols import basic<BR>from twisted.internet import reactor,
protocol</FONT></SPAN></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>import
time</FONT></SPAN></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV><SPAN
class=901561010-16032005><FONT face=Arial size=2>
<DIV><BR><FONT color=#0000ff>class
MyChat(basic.LineReceiver):<BR> def
connectionMade(self):</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff> print "Got a
connection from on " +
time.asctime()<BR>
self.transport.write("Welcome. You are client number :
")<BR> print
"Looping..."<BR> for i in
range(10000000):<BR>
pass<BR> print "Looping
done."<BR>
self.transport.loseConnection()</FONT></DIV>
<DIV><BR><FONT color=#0000ff> def connectionLost(self,
reason):<BR> pass<BR></FONT></DIV>
<DIV><FONT color=#0000ff> def lineReceived(self,
line):<BR> for c in
self.factory.clients:<BR>
c.message(line)</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff> def message(self,
message):<BR>
self.transport.write(message + '\n')</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff> def __init__ (self)
:<BR> self.x =
1<BR> self.duration = 60</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff>print "Starting Twisted Async
Server"<BR>print</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff>factory = protocol.ServerFactory()<BR>factory.protocol
= MyChat<BR>factory.clients = []</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT
color=#0000ff>reactor.listenTCP(7878,factory,10)<BR>reactor.run()<BR></FONT>
</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>#
=================================================================================</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>#
Synchronous socket server code </FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>#
=================================================================================</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>from
socket import *<BR>import sys<BR>import time</FONT></SPAN></DIV>
<DIV><FONT color=#ff0000></FONT> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>s =
socket(AF_INET, SOCK_STREAM)<BR>s.bind(('',
7878))<BR>s.listen(10)</FONT></SPAN></DIV>
<DIV><FONT color=#ff0000></FONT> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>print
'Login Server started on port 7878' </FONT></SPAN></DIV>
<DIV><FONT color=#ff0000></FONT> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#ff0000 size=2>x =
1</FONT></SPAN></DIV>
<DIV><FONT color=#ff0000></FONT> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial size=2><BR><FONT
color=#ff0000>while 1:<BR> client,addr =
s.accept()<BR> print str(x) + ". Got a conection from " +
str(addr) + " on " + time.asctime()<BR> print
"Looping..." <BR> for i in
range(10000000):<BR>
pass<BR> print "Loop done. Ready for
connection..."<BR> print<BR> client.sendall
("Welcome. You are client number : " + str(x))<BR> x +=
1</FONT></FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>#
=================================================================================</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>#
Socket client code</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT face=Arial color=#0000ff size=2>#
=================================================================================</FONT></SPAN></DIV>
<DIV><SPAN class=901561010-16032005><FONT
color=#0000ff> </FONT></SPAN></DIV>
<DIV><FONT face=Arial color=#0000ff size=2>from socket import *<BR>import
sys<BR>import time</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2>s = socket(AF_INET,
SOCK_STREAM)</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2>print 'Creating 20 clients...'
</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2>x = 1<BR>duration = 60<BR>for i in
range(20):<BR> print "Client " + str(x) + "." + " connecting
to localhost at " + time.asctime()<BR> s = socket(AF_INET,
SOCK_STREAM)<BR> s.connect
(("127.0.0.1",7878))<BR> print "Client Connected..." ,
time.asctime()<BR>
s.sendall("Hello\r\n")<BR> data =
s.recv(1024)<BR> print data<BR> x += 1
<BR> s.close()</FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Arial size=2>Regards,<BR><SPAN
class=901561010-16032005></SPAN>D<SPAN
class=901561010-16032005>anny</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV></BODY></HTML>