对python捕获ctrl+c手工中断程序的两种方法详解
时间:2021-06-04 07:54:20|栏目:Python代码|点击: 次
日常编写调试运行程序过程中,难免需要手动停止,以下两种方法可以捕获ctrl+c立即停止程序
1、使用python的异常KeyboardInterrupt
try:
while 1:
pass
except KeyboardInterrupt:
pass
2、使用signal模块
def exit(signum, frame):
print('You choose to stop me.')
exit()
signal.signal(signal.SIGINT, exit)
signal.signal(signal.SIGTERM, exit)
while 1:
pass
上一篇:python爬虫之urllib,伪装,超时设置,异常处理的方法
栏 目:Python代码
下一篇:python实现从字符串中找出字符1的位置以及个数的方法
本文标题:对python捕获ctrl+c手工中断程序的两种方法详解
本文地址:http://www.codeinn.net/misctech/135135.html






