Thread: Lag Problem?

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Lag Problem?


    Code:
    while True:
        try:
            parse_data(s.recv(1024))
            IRCs.readbuffer = IRCs.readbuffer + irc.recv(1024)
            tmp = string.split(IRCs.readbuffer, '\n')
            IRCs.readbuffer = tmp.pop()
            for line in tmp:
                line = string.rstrip(line)
                line = string.split(line)
                if line[0] == 'PING':
                    irc.send('PONG %s\r\n' % line[1])
            print irc.recv(1024)
            parse_data(s.recv(1024))
        except:
            print traceback.print_exc()
    Is this loop sending too much information by any chance? While Battle.net is connected and so is the IRC, could the IRC be interferring with the data that the socket connected to battle.net is trying to receive? Because I've noticed that Battle.net stops receiving data about 30 seconds after IRC has been connected.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    interleaving communication with two sockets like this is not a good idea. If the sockets are in blocking mode (the default) then a call to recv will wait until it has received 1024 bytes, the socket times out (which may be never if the timout is set to None, the default), or the connection is closed.

    So if the IRC connection is not sending any data then the call to irc.recv will never return, and the program will hang.

    There are a couple of ways round this problem. The first is to have a multi-threaded program with separate threads for communicating with the two different sockets, and a queue for passing messages between them. The second is to use an asynchronous framework such as the asyncore & asynchat libraries that come with Python or the twisted framework that builds on those libraries. I believe that twisted already comes with an IRC module, so may have done half your work for you.

    I suggest you have a good look at the asyncore/chat libraries and rethink your whole approach, since to be honest the way you are currently doing it is doomed to failure.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo