下面是一些精简的代码,演示了我对线程的使用:
import threading
import Queue
import time
def example():
""" used in MainThread as the example generator """
while True:
yield 'asd'
class ThreadSpace:
""" A namespace to be shared among threads/functions """
# set this to True to kill the threads
exit_flag = False
class MainThread(threading.Thread):
def __init__(self, output):
super(MainThread, self).__init__()
self.output = output
def run(self):
# this is a generator that contains a While True
for blah in example():
self.output.put(blah)
if ThreadSpace.exit_flag:
break
time.sleep(0.1)
class LoggerThread(threading.Thread):
def __init__(self, output):
super(LoggerThread, self).__init__()
self.output = output
def run(self):
while True:
data = self.output.get()
print data
def main():
# start the logging thread
logging_queue = Queue.Queue()
logging_thread = LoggerThread(logging_queue)
logging_thread.daemon = True
logging_thread.start()
# launch the main thread
main_thread = MainThread(logging_queue)
main_thread.start()
try:
while main_thread.isAlive():
time.sleep(0.5)
except KeyboardInterrupt:
ThreadSpace.exit_flag = True
if __name__ == '__main__':
main()
我有一个主线程,它获取从阻塞生成器生成的数据。在实际代码中,该生成器生成通过套接字嗅探出来的网络相关数据。
然后我有一个日志记录、守护进程、线程,它将数据打印到屏幕上。
为了彻底退出程序,我捕获了一个KeyboardInterrupt
,它将设置一个exit_flag
来尝试 - 这告诉主线程返回。
十有八九,这样就可以正常工作。程序将干净退出。但是,有时我会收到以下两个错误:
错误1:
^CTraceback (most recent call last):
File "demo.py", line 92, in <module>
main('')
File "demo.py", line 87, in main
time.sleep(0.5)
KeyboardInterrupt
错误2:
Exception KeyboardInterrupt in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
我已经运行了这个确切的示例代码几次,但无法复制错误。此代码与真实代码之间的唯一区别是 example()
生成器。正如我所说,这会从套接字生成网络数据。
你能看出我处理线程的方式有什么问题吗?
最佳答案
键盘中断
由 arbitrary 接收线程。如果接收者不是主线程,它就会终止,主线程不受影响,ThreadSpace.exit_flag
保持 false,并且脚本继续运行。
如果您希望 sigint
工作,您可以让每个线程捕获 KeyboardInterrupt
并调用 thread.interrupt_main()
让Python退出,或者使用signal
模块,如官方文档所述。
关于python - 我的 KeyboardInterrupt 仅在 90% 的情况下被捕获。我在什么方面失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988932/