python - 如何退出正在运行线程的Python程序?

标签 python multithreading exit

我正在编写一些简单的代码,我使用 Pygame 进行图形处理,并且我有一个线程可以绘制所有内容。当我调用 sys.exit() 或只是 ctrl+c 时,主程序退出,但线程似乎仍然存在。我想我需要先把它关掉。我该怎么做?

示例:

import threading

class Bla(threading.Thread):
    def run(self):
        print "I draw here"

Bla().start()

while True:
     if user_wants_to_end:
         sys.exit()

当我确实想退出时,程序不会退出!如何关闭线程?

最佳答案

Python 程序在所有非守护线程完成后退出。

class Bla(threading.Thread):
    daemon = True # Make the thread daemon
    def run(self):
        print "I draw here"

关于python - 如何退出正在运行线程的Python程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6694916/

相关文章:

python - 在 pandas 中为嵌套字典创建列名

python - aws eb cli Windows 在 colorama 上出现版本错误

python - 如何找到最大的2个数字?

Python - SQLAlchemy - MySQL - 多个实例处理相同的数据

c# - WindowsFormsSynchronizationContext.Current.Post 在后台线程上执行 lambda

multithreading - 为什么不在 fill_n 中调用移动构造函数

java - OS X 上的 JVM 总是以状态 0 退出(如 Bash 脚本中捕获的那样)?

c++ - 在调用退出后访问其他线程中的全局变量是否安全?

python - 获取 "AttributeError: ' float'对象没有属性 'replace'“替换字符串时出错

Android,使用 : android. 关闭应用程序 os.Process.killProcess(android.os.Process.myPid());