Close listening socket in python thread

后端 未结 5 961
一生所求
一生所求 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:43

    A dirty solution which allows to exit your program is to use os._exit(0).

    def stop(self):
        self.socket.close()
        os._exit(0)
    

    note that sys.exit doesn't work/blocks as it tries to exit cleanly/release resources. But os._exit is the most low level way and it works, when nothing else does.

    The operating system itself will release the resources (on any modern system) like when doing exit in a C program.

提交回复
热议问题