SockerServer.TCPServer problem

H

huy

Hi All,

Can someone explain why this bit of code can't keep up with some java
code which spawns about 200 (threads) simultaneous connections each
sending about 10 mesgs each. I basically don't get the 2000 connections
i expect on the python side. I end up with some number very close on
different runs eg. 1989, 1972, 1992 etc.

Any ideas ?

Huy

#!/usr/local/bin/python

import socket
import struct
import time
import SocketServer
import thread
from datetime import datetime

import os
import datetime
import random

LISTENING_PORT = 1600

cnt = 0

class PDAHandler(SocketServer.BaseRequestHandler):
def handle(self):
global cnt

cnt += 1
s = self.request
dlen = s.recv(4, socket.MSG_WAITALL)
print "Received: ", dlen, cnt, " from ", s.getpeername()
dataLen = struct.unpack("!i", dlen)
data = s.recv(dataLen[0])
f = file("aaa/recv%02d.txt" % cnt, "w+")
f.write(data)
f.close()


def start_servers():
msg_server = SocketServer.TCPServer(("", LISTENING_PORT), PDAHandler)
msg_server.serve_forever()

if __name__ == "__main__":
start_servers()
 
J

Jeremy Jones

huy said:
Hi All,

Can someone explain why this bit of code can't keep up with some java
code which spawns about 200 (threads) simultaneous connections each
sending about 10 mesgs each. I basically don't get the 2000
connections i expect on the python side. I end up with some number
very close on different runs eg. 1989, 1972, 1992 etc.

<snip>

(Sorry if I post this twice - I'm trying out a new mail client.)

You may want to try using the ThreadingTCPServer class rather than
TCPServer. Just change this line:
msg_server = SocketServer.TCPServer(("", LISTENING_PORT), PDAHandler)


to this:

msg_server = SocketServer.ThreadingTCPServer(("", LISTENING_PORT),
PDAHandler)

BTW - how are you tracking how many connections you have open with the
Python server? It may be that the process accepts the connection, puts
them into a queue to be handled, and waits for the deals with them as it
can. Or is the 2000 the total number of connections that the 200
threads produce over the whole run (each making 10 connections)?


Jeremy Jones
 
H

huy

Jeremy said:
<snip>

(Sorry if I post this twice - I'm trying out a new mail client.)

You may want to try using the ThreadingTCPServer class rather than
TCPServer. Just change this line:




to this:

msg_server = SocketServer.ThreadingTCPServer(("", LISTENING_PORT),
PDAHandler)

Thanks, I'll try that.
BTW - how are you tracking how many connections you have open with the
Python server? It may be that the process accepts the connection, puts
them into a queue to be handled, and waits for the deals with them as it
can. Or is the 2000 the total number of connections that the 200
threads produce over the whole run (each making 10 connections)?

every connection creates a file. i count the number of files created.
 
D

Dennis Lee Bieber

every connection creates a file. i count the number of files created.
And how are the file names generated? Is there no chance that
two threads may not generate the same name and overwrite each other?

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top