当前位置:主页 > 软件编程 > Python代码 >

对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

您可能感兴趣的文章:

相关文章