Stop python using ctrl + c

I have a python script that uses streams and makes a lot of HTTP requests. I think what happens when an HTTP request is read (using urllib2), it blocks and does not respond to Ctrl C to stop the program. Is there any way around this?

+108
python
Sep 01 '09 at 19:17
source share
12 answers

On Windows, the only sure way is to use Ctrl Break . Stopping every python script instantly!

(Note that on some keyboards, “Gap” is indicated as “Pause.”)

+152
Sep 01 '09 at 19:21
source share

Pressing Ctrl + c while the program is running in Python will cause python to throw a KeyboardInterrupt exception. It is likely that a program that makes many HTTP requests will have a lot of exception handling code. If the except of the try part - the except block does not determine what exceptions it should catch, it will catch all exceptions, including the KeyboardInterrupt that you just called. A properly encoded Python program will use the Python exception hierarchy and catch only those exceptions that are received from Exception .

 #This is the wrong way to do things try: #Some stuff might raise an IO exception except: #Code that ignores errors #This is the right way to do things try: #Some stuff might raise an IO exception except Exception: #This won't catch KeyboardInterrupt 

If you cannot change the code (or you need to kill the program for the changes to take effect), you can quickly press Ctrl + c . The first of the KeyboardInterrupt exceptions will knock your program out of the try block and hopefully one of the later KeyboardInterrupt exceptions will be thrown when the program is outside the try block.

+66
Sep 01 '09 at 20:12
source share

If it is running in the Python shell, use Ctrl + Z , otherwise find the python process and kill it.

+47
Sep 01 '09 at 19:18
source share

The interrupt process is hardware and OS dependent. This way you will have a completely different behavior depending on where you run your python script. For example, on Windows machines we have Ctrl + C ( SIGINT ) and Ctrl + Break ( SIGBREAK ).

Thus, although SIGINT is present on all systems and can be processed and caught, the SIGBREAK signal is Windows-specific (and can be disabled in CONFIG.SYS) and is really processed by the BIOS as an INT 1Bh interrupt vector, so this key is much more powerful than any other. Therefore, if you use some OS other than * nix, you will get different results depending on the implementation, since this signal is not there, but others. On Linux, you can check which signals are available to you:

 $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE 9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG 17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD 21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGPWR 30) SIGUSR1 31) SIGUSR2 32) SIGRTMAX 

So, if you want to catch CTRL+BREAK signal on a Linux system, you will need to check that POSIX , they displayed this key. Popular mappings:

 CTRL+\ = SIGQUIT CTRL+D = SIGQUIT CTRL+C = SIGINT CTRL+Z = SIGTSTOP CTRL+BREAK = SIGKILL or SIGTERM or SIGSTOP 

In fact, many other functions are available on Linux, where the SysRq key (system request) can take its own life ...

+29
Jan 30 '14 at 15:05
source share

This post is deprecated, but I recently ran into the same CTRL+C problem without ending with python scripts on my Linux . I used CTRL + \ ( SIGQUIT ).

+16
Nov 20 '16 at 12:28
source share

Ctrl + C Difference for Windows and Linux

It turns out that in Python 3.6, the interpreter handles the SIGINT generated by Ctrl + C differently for Linux and Windows. On Linux, ctrl + c will work mostly as expected, however on Windows, ctrl + c will not work if the call is blocked, for example, thread.join or waiting for a response on the Internet (however, it works for time.sleep , However). Here's a good explanation of what is happening in the Python interpreter.

Solution 1. Use Ctrl + Break or Equivalent

Use the keyboard shortcuts in the terminal / console window below that will generate a lower level SIGBREAK in the OS and terminate the Python interpreter.

Mac OS and Linux

Ctrl + Shift +\or Ctrl + \

Windows :

  • General: Ctrl+Break
  • Dell: Ctrl+Fn+F6 or Ctrl+Fn+S
  • Lenovo: Ctrl+Fn+F11 or Ctrl+Fn+B
  • HP: Ctrl+Fn+Shift
  • Samsung: Fn+Esc

Solution 2. Use the Windows API

The following are convenient functions that Windows will detect and install a custom handler for Ctrl + C in the console:

 #win_ctrl_c.py import sys def handler(a,b=None): sys.exit(1) def install_handler(): if sys.platform == "win32": import win32api win32api.SetConsoleCtrlHandler(handler, True) 

You can use the above like this:

 import threading import time import win_ctrl_c # do something that will block def work(): time.sleep(10000) t = threading.Thread(target=work) t.daemon = True t.start() #install handler install_handler() # now block t.join() #Ctrl+C works now! 

Solution 3: Polling Method

I do not prefer or recommend this method, because it unnecessarily consumes the processor and power, which negatively affect performance.

import time

 def work(): time.sleep(10000) t = threading.Thread(target=work) t.daemon = True t.start() while(True): t.join(0.1) #100ms ~ typical human response # you will get KeyboardIntrupt exception 
+6
Oct 23 '18 at 5:31
source share

On Mac Press:

"control" + "\"

to exit the python process connected to the terminal.

+4
Jan 17 '18 at 14:14
source share

On mac / in Terminal:

  • Show Inspector (right-click in a terminal window or Shell> Show Inspector)
  • click the "Settings" icon above the "running processes"
  • select from the list of options in the "Signal Processing Group" section (Kill, complete, interrupt, etc.).
+1
Mar 14 '17 at 17:07
source share
  • Forcing a program to close with Alt + F4 (turns off the current program)
  • Spam X buttons on CMD for ex
  • Taskmanager (first Windows + R, then "taskmgr"), and then completes the task.

This can help.

0
Oct 05 '18 at 19:44
source share

For reference, what killed the process on my Raspberry 3B + (raspbian works) was:

 CTRL + ' 

On my French keyboard, the AZERTY touch is also number 4.

0
Jan 22 '19 at 20:57
source share

You can open your task manager (ctrl + alt + delete, then go to the task manager) and view it in python, and the server is called (for example) _go_app (naming convention: _language_app).

If I complete the _go_app task, it will shut down the server, therefore, going there in the browser, it will say that it "unexpectedly terminated", I also use git bash, and when I start the server, I can not exit the server in the bash shell . using ctrl + c or ctrl + pause, but as soon as you complete the python task (the one that uses 63.7 mb), it will exit the server script in bash and allow me to use the git bash shell. enter image description here

0
Apr 14 '19 at 20:28
source share

ctrl + q will instantly stop the current code and close all windows.

-one
Aug 6 '17 at 13:15
source share



All Articles