Slow Python and Sockets in windows

R

Rami Saarinen

I have been making a client-server type of software mainly on Linux and
now I am trying to get it all work also on windows. However, I am having
slight problems running the program in Windows 2000. I have tried Python
2.2.3 and the latest release 2.3.1 (?)

If I have 2 clients and 1 server. The server listening some predefined
port and all the sending (in client and server) is done by creating a
new connection. On the beginning the client "logs on" to the server.
Now, starting the server and first client is ok, but when I try to start
the second client (all on the same machine) it takes 35 seconds to
start!!? And most of the time seems to go on starting python itself (it
does not even reach the first line of __init__), not my program or the
networking stuff. All the data goes through the server.

The connections are TCP and the sending of message is ended by
shutdown(2) and close() both in the server and client end. The server
extends ThreadingMixIn and TCPServer and uses very simple Handler that
extends StreamRequestHandler and overrides the handle method simply
appending the received data into a string and calling a method of the
server with the string as parameter. The server and client use ports >
8000.

Should I close the socket at the Handler end when the sender as closed
the connection?

There were also broken connections ("connection refused") until I
changed the connection to connect_ex and kept trying until it succeeded
(not good, but I made it to see if it helps). The console windows, on
which I run the server and clients, are very slow and the printouts may
take a while before appearing (some speed optimization by MS?) and the
socket operations seem to be slow. I had none of these problems on
Linux.

Am I missing something or what is going on?


Here's the handler:

class ChannelListener(StreamRequestHandler):
def handle(self):
req=""
buffer=self.request.recv(512)
print self.request
while len(buffer)>0:
req+=str(buffer)
buffer=self.request.recv(512)
self.server.processRequest(self.request,req)
return


and the send method of server:

def send(self,container,msg):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# some lines removed, basically just doing:
connection_data=(ip,port) # ip & port comes from a list
pirunmuuttuja=1
while(pirunmuuttuja!=0):
pirunmuuttuja=s.connect_ex(connection_data)
s.send(msg)
s.shutdown(2)
s.close()

and the send method of the client:

def send(self,msg):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
pipa=1
while(pipa!=0):
pipa=s.connect_ex(self.server)
s.send(msg)
s.shutdown(2)
s.close()

I'd appreciate any feedback (other than "your code sucks!" ;) ).

Thanks!
 
M

MetalOne

I don't see the problem in this snippet of code.
However, the connect calls should succeed without the need to
continually retry.
The 35 second startup sounds like something is waiting for a timeout
to complete.
The recv() calls are blocking calls by default.
Could you be blocked in a recv() call.
I can't see if you are using threads to handle each client
communication.

You have also ommitted how you are accepting connections. Perhaps the
server may be busy and not ready to accept connections.

Have you looked into using asyncore and asynchat. These modules make
TCP/IP communication very easy.
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top