Quit Python execution with Ctrl-C (responds to SIGINT)

Once you've done heavy math in Python, you'll have trouble getting Ctrl-C to stop:

$ python3 cext09.py
^C^C^C^C^C^C #do not stop

Ctrl-Z works, so there's a way to interrupt and 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

This is tedious and may kill the wrong one.

signal

Just write this in a Python script and you're good to go.

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

Recommended Posts

Quit Python execution with Ctrl-C (responds to SIGINT)
How to measure execution time with Python Part 1
How to measure execution time with Python Part 2
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Execution time measurement with Python With
Switch python to 2.7 with alternatives
Write to csv with Python
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
Python (from first time to execution)
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
Easily post to twitter with Python 3
I want to debug with Python
Easy parallel execution with python subprocess
How to build Python and Jupyter execution environment with VS Code
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
Convert memo at once with Python 2to3
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
I wanted to solve ABC160 with Python
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
[Introduction to Python] Let's use foreach with Python
Single pixel camera to experience with Python
[Python] Introduction to CNN with Pytorch MNIST
Convert FX 1-minute data to 5-minute data with Python
I want to analyze logs with Python
How to do portmanteau test with python
I want to play with aws with python
How to display python Japanese with lolipop
Trying to handle SQLite3 with Python [Note]
[Part1] Scraping with Python → Organize to csv!
Connect to s3 with AWS Lambda Python
Add Gaussian noise to images with python2.7
How to enter Japanese with Python curses
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Connect to pepper with PEPPER Mac's python interpreter