[Twisted-Python] How to accept connections faster
Sinang, Danny
D.Sinang at spitech.com
Wed Mar 16 03:21:47 MST 2005
Hello.
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.
Can anyone here suggest any code improvements for my Twisted server to
accept connections faster ?
I've include the Synchronous socket server code and the client generator
code below as well.
#
========================================================================
==================
# Twisted server code
#
========================================================================
==================
from twisted.protocols import basic
from twisted.internet import reactor, protocol
import time
class MyChat(basic.LineReceiver):
def connectionMade(self):
print "Got a connection from on " + time.asctime()
self.transport.write("Welcome. You are client number : ")
print "Looping..."
for i in range(10000000):
pass
print "Looping done."
self.transport.loseConnection()
def connectionLost(self, reason):
pass
def lineReceived(self, line):
for c in self.factory.clients:
c.message(line)
def message(self, message):
self.transport.write(message + '\n')
def __init__ (self) :
self.x = 1
self.duration = 60
print "Starting Twisted Async Server"
print
factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
reactor.listenTCP(7878,factory,10)
reactor.run()
#
========================================================================
=========
# Synchronous socket server code
#
========================================================================
=========
from socket import *
import sys
import time
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', 7878))
s.listen(10)
print 'Login Server started on port 7878'
x = 1
while 1:
client,addr = s.accept()
print str(x) + ". Got a conection from " + str(addr) + " on " +
time.asctime()
print "Looping..."
for i in range(10000000):
pass
print "Loop done. Ready for connection..."
print
client.sendall ("Welcome. You are client number : " + str(x))
x += 1
#
========================================================================
=========
# Socket client code
#
========================================================================
=========
from socket import *
import sys
import time
s = socket(AF_INET, SOCK_STREAM)
print 'Creating 20 clients...'
x = 1
duration = 60
for i in range(20):
print "Client " + str(x) + "." + " connecting to localhost at " +
time.asctime()
s = socket(AF_INET, SOCK_STREAM)
s.connect (("127.0.0.1",7878))
print "Client Connected..." , time.asctime()
s.sendall("Hello\r\n")
data = s.recv(1024)
print data
x += 1
s.close()
Regards,
Danny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20050316/320c8c3d/attachment.html>
More information about the Twisted-Python
mailing list