Slow Python and Sockets in windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rami Saarinen

    Slow Python and Sockets in windows

    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 StreamRequestHa ndler 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 (StreamRequestH andler):
    def handle(self):
    req=""
    buffer=self.req uest.recv(512)
    print self.request
    while len(buffer)>0:
    req+=str(buffer )
    buffer=self.req uest.recv(512)
    self.server.pro cessRequest(sel f.request,req)
    return


    and the send method of server:

    def send(self,conta iner,msg):
    s=socket.socket (socket.AF_INET , socket.SOCK_STR EAM)
    # some lines removed, basically just doing:
    connection_data =(ip,port) # ip & port comes from a list
    pirunmuuttuja=1
    while(pirunmuut tuja!=0):
    pirunmuuttuja=s .connect_ex(con nection_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_STR EAM)
    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!

    --
    Rami Saarinen

    if you would like to aswer by email, plese do remove the obvious part
    from my email address.
  • MetalOne

    #2
    Re: Slow Python and Sockets in windows

    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.

    Comment

    Working...