线程中的变量不会随类的变量而变化

时间:2017-12-06 21:40:32

标签: python

我的服务器类正在侦听客户端:

def listen_for_clients(self):
    self.sock.settimeout(1)
    while 1:
        try:
            print 'listening for clients'
            print self.quit_time
            if self.quit_time: break
            conn, addr = self.sock.accept()
            conn.setblocking(1)
            self.connections.append(conn)
            thread.start_new_thread(self.client_listener,(conn, ))  
        except socket.timeout:
            pass

然而,当我将boolean self.quit_time更改为True时,它仍然在线程中打印为false并且不会中断。这是为什么?

这是改变self.quit_time值的代码:

def close(self):
    self.quit_time = True
    time.sleep(1)
    print 'closing:', self
    if self.incoming_thread:
        self.incoming_thread.join()
        print 'incoming thread joined'
    if self.outgoing_thread:
        self.outgoing_thread.join()
        print 'outgoing thread joined'
    if self.listener_thread:
        self.listener_thread.join()
        print 'listener thread joined'
    for c in self.connections:
        if c:
            c.close()
    if self.sock:
        self.sock.close()

0 个答案:

没有答案