18
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python の実行を Ctrl-C で終了させる(SIGINTに反応する)

Last updated at Posted at 2016-08-01

Python で重い計算を一度実行すると Ctrl-C でなかなか止まらなくて困ることがある:

$ python3 cext09.py
^C^C^C^C^C^C # 止まらない

Ctrl-Z は効くので、それで中断して kill するという手はある:

^Z
zsh: suspended  python3 cext09.py
$ jobs
[1]  - running    emacs cext09.c
[2]  + suspended  python3 cext09.py
$ kill %2
[2]  + terminated  python3 cext09.py

これは面倒だし、間違ったものを kill するかもしれない。

signal

これを Python スクリプトに書いておけばOK。

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
18
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?