By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

SockerServer.TCPServer problem

P: n/a
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()
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
huy wrote:
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
Jul 18 '05 #2

P: n/a
huy
Jeremy Jones wrote:
huy wrote:
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)


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.
Jeremy Jones

Jul 18 '05 #3

P: n/a
On Wed, 18 Aug 2004 01:28:39 +1000, huy <ny*****@swiftdsl.com.au>
declaimed the following in comp.lang.python:

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?

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.