7

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 '\nkeyboardinterrupt found!'
    print '\n...Program Stopped Manually!'
    raise

繰り返しますが、何が問題なのかはわかりませんが、私の端末は、例外にある 2 つの印刷アラートを出力することさえありません。誰かがこの問題を理解するのを手伝ってくれますか?

4

1 に答える 1

15

以下のようにbreak、ステートメントをステートメントに置き換えます。raise

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 '\nkeyboardinterrupt caught (again)'
    print '\n...Program Stopped Manually!'
    raise

ブロック内の 2 つの print ステートメントは、except'(again)' が 2 番目に表示された状態で実行されます。

于 2011-12-27T15:03:57.413 に答える