0
我一直在尋找停止用戶中斷後的線程,但由於某種原因,它不適合我。任何人都可以幫忙問題在於程序忽略了鍵盤中斷錯誤,在鍵盤中斷後沒有關閉。如何使用threading.event函數殺死一個線程?
#!/usr/bin/env python
#
#
from time import sleep
from Queue import Queue
from threading import Thread,Event,Lock
def Count():
global Exit
for i in range(5):
try:
if not Exit.is_set():
with l:
print i;sleep(2)
except KeyboardInterrupt:
Exit.set()
if __name__ == '__main__':
l = Lock()
q = Queue()
Exit = Event()
for i in range(2):
Bot = Thread(target=(Count)).start()
q.put(Bot)
#q.join()
#OutPut
0
1
^C2
3
4
0
Exception KeyboardInterrupt in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
你可以分享什麼是錯誤和預期的行爲? – Fejs