Python - 带套接字的线程不会停止

时间:2014-08-24 21:31:38

标签: python multithreading sockets python-3.x

我正在编写一个简单的基于服务器的聊天程序。我正在使用线程。一切正常,但要停止程序,我必须手动终止它,因为正在搜索新用户的线程不会终止,因为socket.accep()函数仍在运行。我试图解决这个问题,通过关闭套接字,但不知何故,这没有成功。这是我的用户搜索主题代码:

import socket, time
from threading import Thread
from userHandling import UserHandler

class UserSearcher(Thread):

    def __init__(self):
        print("Thread: User searching is will start now!")
        Thread.__init__(self)

    def run(self):
        self.__uH = UserHandler()
        self.__tcpListener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.__tcpListener.bind(("", 2030))
        self.__tcpListener.listen(5)
        while True:
            try:
                self.__komm, self.__tcpAddr = self.__tcpListener.accept()
                self.__tcpMsg = self.__komm.recv(1024)
                self.__uH.add(self.__tcpMsg.decode(), self.__tcpAddr[0])
            except:
                print("Searching for chat members failed! Is the system shutting down?")
                break

    def stop(self):
        self.__tcpListener.close()
        time.sleep(1)
        print("Thread: User searching will quit NOW!")
        pass

我没有看到错误:/ 感谢python初学者

2 个答案:

答案 0 :(得分:1)

使用socket.shutdown()代替close()

import socket, time
from threading import Thread

class UserSearcher(Thread):

    def __init__(self):
        print("Thread: User searching is will start now!")
        Thread.__init__(self)

    def run(self):
        self.__tcpListener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.__tcpListener.bind(("", 2030))
        self.__tcpListener.listen(5)
        while True:
            try:
                self.__komm, self.__tcpAddr = self.__tcpListener.accept()
                self.__tcpMsg = self.__komm.recv(1024)
                print(self.__tcpMsg)
            except:
                print("Searching for chat members failed! Is the system shutting down?")
                break

    def stop(self):
        self.__tcpListener.shutdown(socket.SHUT_RDWR)
        time.sleep(1)
        print("Thread: User searching will quit NOW!")

答案 1 :(得分:0)

如何为while循环设置标志:

class UserSearcher(Thread):

    def __init__(self):
        print("Thread: User searching is will start now!")
        Thread.__init__(self)

    def run(self):
        self.__uH = UserHandler()
        self.__tcpListener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.__tcpListener.bind(("", 2030))
        self.__tcpListener.listen(5)
        self.running = True
        while self.running:
            try:
                self.__komm, self.__tcpAddr = self.__tcpListener.accept()
                self.__tcpMsg = self.__komm.recv(1024)
                self.__uH.add(self.__tcpMsg.decode(), self.__tcpAddr[0])
            except:
                print("Searching for chat members failed! Is the system shutting down?")
                break

    def stop(self):
        self.__tcpListener.close()
        self.running = False
        time.sleep(1)
        print("Thread: User searching will quit NOW!")
        pass