zoukankan      html  css  js  c++  java
  • python退出无限循环与KeyboardInterrupt异常

    参考:http://www.voidcn.com/article/p-pmlncsni-bvo.html

    按下Ctrl C时,我的while循环不会退出.它似乎忽略了我的KeyboardInterrupt异常.循环部分如下所示:
    while True:
      try:
        if subprocess_cnt <= max_subprocess:
          try:
            notifier.process_events()
            if notifier.check_events():
              notifier.read_events()
          except KeyboardInterrupt:
            notifier.stop()
            break
        else:
          pass
      except (KeyboardInterrupt, SystemExit):
        print '
    keyboardinterrupt found!'
        print '
    ...Program Stopped Manually!'
        raise

    同样,我不确定问题是什么,但我的终端甚至从未打印过我的异常中的两个打印警报.有人能帮我解决这个问题吗?

     
    用raise语句替换break语句,如下所示:
    while True:
      try:
        if subprocess_cnt <= max_subprocess:
          try:
            notifier.process_events()
            if notifier.check_events():
              notifier.read_events()
          except KeyboardInterrupt:
            notifier.stop()
            print 'KeyboardInterrupt caught'
            raise  # the exception is re-raised to be caught by the outer try block
        else:
          pass
      except (KeyboardInterrupt, SystemExit):
        print '
    keyboardinterrupt caught (again)'
        print '
    ...Program Stopped Manually!'
        raise

    except块中的两个print语句应该以'(再次)’出现的第二个执行.

     
     
  • 相关阅读:
    T-SQL 关闭数据库所有连接
    单页web应用(SPA)的简单介绍
    ES6—解构赋值
    ES6 — 新增关键字let、const
    一行能装逼的JavaScript代码
    Date 对象总结
    JS从头开始
    CSS基础知识点(二)——居中
    web标准的可用性和可访问性
    CSS基础知识点(二)——选择器
  • 原文地址:https://www.cnblogs.com/kuangke/p/14702233.html
Copyright © 2011-2022 走看看