import threading
import time
import sys
def func():
while 1:
print 'test'
time.sleep(1)
class MyThread(threading.Thread):
def __init__(self,func):
threading.Thread.__init__(self)
self.func = func
def run(self):
self.func()
if __name__ == '__main__':
T = MyThread(func)
T.setDaemon(True)
T.start()
time.sleep(3)
sys.exit(0)
代码如上,想把time.sleep(3)替换成捕捉Ctrl+C来进行结束,在Linux下也能用,求大神指教
Ctrl + C 是默认终止清虚符,所以捕获KeyboardError即可,跨搜稿平台,windows和linux通用,都会捕获到键盘终止程序,不管是答漏燃Ctrl+C还是Ctrl+D等等、、
Try:
#you code
pass
except KeyBoardError:
#you code
pass