Unexpected (?) Thread behaviour

Vincent Berg gorny0 at zonnet.nl
Fri May 2 17:15:51 EDT 2003


I'm having some problems with threads using Python2.2 under WindowsXP
(btw it doesn't work under Linux either). I'm curious if this is the
desired behaviour of the Python threads or not. The following thread
is running and it's serving all the clients in the list self.clients.

class ServerThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.clients = []

    def run(self):
        while 1:
            if len(self.clients) > 0:
                rd, wd, ex = select.select(self.clients, [], [], None)
                for n in rd:
                    n.handle()

    def add_client(self, sock, addr):
        self.clients.append(ClientHandler(sock, addr))

But another (main) thread calls the method add_client of the instance
of ServerThread. But ServerThread continues to run in the loop and
thus it
didn't notice self.clients has now a member more. I've checked the
length of
self.clients after adding a client in add_client and it's nicely
increased.

So my actual question is: why doesn't the run() method notices that
self.clients is updated?? Shouldn't it notice it?? Of course I could
fix it by stopping the thread while no clients are in it anymore and
restarting it when I'm having one, new client available.

Cheers,
Vincent




More information about the Python-list mailing list