小编Col*_*rgi的帖子

Python 线程 run() 阻塞

当我创建以下内容时,我试图创建一个可以被异常终止的线程类(因为我试图让线程等待一个事件):

import sys

class testThread(threading.Thread):
    def __init__(self):
        super(testThread,self).__init__()
        self.daemon = True

    def run(self):
        try:
            print('Running')
            while 1:
                pass
        except:
            print('Being forced to exit')

test1 = testThread()
test2 = testThread()

print(test1.daemon)
test1.run()
test2.run()

sys.exit()
Run Code Online (Sandbox Code Playgroud)

但是,运行该程序只会打印出一个 Running 消息,直到另一个终止。这是为什么?

python multithreading python-multithreading python-3.x

3
推荐指数
1
解决办法
2470
查看次数