Sunday, May 19, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 29030  / 2 Years ago, tue, november 15, 2022, 4:45:45

I am trying to understand the reason why the whole Python process does not get killed when I press Ctrl + C inside and infinite loop or for that matter any Python function that I am running in terminal and only the loop/function is stopped?


More From » command-line

 Answers
5

It's because of the design of the Python interpreter and interactive session.



Ctrl + C sends a signal, SIGINT, to the Python process, which the Python interpreter handles by raising the KeyboardInterrupt exception in the currently-running scope.



If the interpreter is running in an interactive session (i.e. by running python or python3 at the console), then the exception in the current function is printed and you return to the Python prompt. If the interpreter is running a script (e.g. by python3 my_script.py), then unless the KeyboardInterrupt is handled by the script, the whole program will stop when the exception is raised.


[#3945] Wednesday, November 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ippalogu

Total Points: 215
Total Questions: 127
Total Answers: 146

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;