Multiple Threads & IRC

Erno Kuusela erno-news at erno.iki.fi
Sun Dec 10 21:44:47 EST 2000


In article <3A3415E0.1080607 at bellsouth.net>, Wilbur
<willybur at bellsouth.net> writes:

| It seems that I will need multiple threads running for this client.  One 
| thread will have to be constantly checking my socket to see if the 
| server has sent anything.

it is probably easier to use the Tkinter.tkinter.createfilehandler().
that will let you pass in a callback function that will get called
once there is input on a socket.

| The server sends data in packets, and it sends whenever it can.  How can 
| I get my client to be able to receive data from the server without 
| knowing when it will send and what amount of data will be sent?  I only 
| know how to tell Python to wait until it receives, say, 1024 bytes.  If 
| the server doesn't send any more, how can I tell Python to stop waiting? 

you have to employ some buffering to make the pieces of data arriving
from the server break into lines nicely. something like
(untested, off the top of my head)

def input_callback(self, sock):
        data = sock.read(1024)
        # if there's more than 1024 bytes of data coming,
        # we'll just get called again.
        if not data:
                # eof, handle...
        else:      
                self.buf = self.buf + data
                while '\n' in self.buf:
                        line, self.buf = string.split(self.buf, '\n', 1)
                        self.handle_line(line)



you could do it with threads too, i think, but i'm not familiar with
tkinter <-> threads interaction.

  -- erno



More information about the Python-list mailing list