python在脚本运行中通过键盘输入关闭脚本
importthreadingimporttimeimportsysdeffunc():while1:print'test'time.sleep(1)classMyThr...
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下也能用,求大神指教 展开
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下也能用,求大神指教 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询