【发布时间】:2015-04-15 21:01:20
【问题描述】:
我不明白为什么我的 SIGINT 从未被下面的代码捕获。
#!/usr/bin/env python
from threading import Thread
from time import sleep
import signal
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
self.running = True
def stop(self):
self.running = False
def run(self):
while self.running:
for i in range(500):
col = i**i
print col
sleep(0.01)
global threads
threads = []
for w in range(150):
threads.append(MyThread())
def stop(s, f):
for t in threads:
t.stop()
signal.signal(signal.SIGINT, stop)
for t in threads:
t.start()
for t in threads:
t.join()
要清理此代码,我宁愿尝试/排除join() 并在出现异常时关闭所有线程,这样行吗?
【问题讨论】:
标签: python multithreading sigint