Close listening socket in python thread

后端 未结 5 960
一生所求
一生所求 2021-01-04 10:07

I have a problem trying to learn about sockets for network communication. I have made a simple thread that listens for connections and creates processes for connecting clien

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 10:40

    One way to get the thread to close seems to be to make a connection to the socket, thus continuing the thread to completion.

    def stop(self):
        self.running = False
        socket.socket(socket.AF_INET, 
                      socket.SOCK_STREAM).connect( (self.hostname, self.port))
        self.socket.close()
    

    This works, but it still feels like it might not be optimal...

提交回复
热议问题