我有一个有两个线程的应用程序。一个是运行简单游戏的 pygame 线程,另一个线程是一个监听服务器,它接受用于控制游戏的消息。
这是精简后的伪代码:
class ServerThread(threading.Thread):
def run(self):
class SingleTCPHandler(SocketServer.BaseRequestHandler):
try:
while(1):
...
#Receive messages from socket. Add them to pygame event queue
...
except KeyboardInterrupt:
sys.exit(0)
...
...
class PygameThread(threading.Thread):
def run(self):
...
#pygame stuff
...
#The following pygame code closed the app when closing the pygame window while running as a single thread
for event in pygame.event.get():
if event.type==QUIT:
exit()
...
try:
server_thread = ServerThread()
server_thread.start()
pygame_thread = PygameThread()
pygame_thread.start()
except KeyboardInterrupt:
sys.exit(0)
似乎没有任何异常被捕获。我试过只运行没有pygame线程的服务器和:
try:
while(1):
...
#Receive messages from socket. Add them to pygame event queue
...
except KeyboardInterrupt:
sys.exit(0)
不回应Ctrl + c
pygame 窗口标准关闭按钮(右边的小 x 操作)不再起作用。
我尝试了一种解决方法:
try:
server_thread = ServerThread()
server_thread.start()
pygame_thread = PygameThread()
pygame_thread.start()
except KeyboardInterrupt:
sys.exit(0)
也不起作用。
我正在寻找关闭应用程序的想法,而不必杀死启动应用程序的外壳。
更新
根据建议,我做了以下事情:将while True
两个胎面中的前者更改为while not self.stop_requested:
.
并且:
try:
pygame_thread = PygameThread()
pygame_thread.start()
server_thread = ServerThread()
server_thread.start()
except KeyboardInterrupt:
pygame_thread.stop_requested = True
server_thread.stop_requested = True
它仍然无法正常工作。我还注意到,当我尝试使用 Ctrl+c 终止时,在运行此代码的控制台中,它只会被打印出来。
alan@alan ~/.../py $ python main.py
^C^C^C^C^C^C^C
更新
我做了一个小快捷方式并将服务器线程更改为守护程序,因此一旦 pygame 窗口(即 pygame 线程)关闭,它就会关闭。