I have a Python script that uses threads and makes lots of HTTP requests.
I think, while a HTTP request (using urllib2) is reading, it's blocking and not responding to Ctrl + C to stop the program. Is there a way around this?
I have a Python script that uses threads and makes lots of HTTP requests.
I think, while a HTTP request (using urllib2) is reading, it's blocking and not responding to Ctrl + C to stop the program. Is there a way around this?
On Windows, the only sure way is to use Ctrl + Break. It stops every Python script instantly!
(Note that on some keyboards, "Break" is labeled as "Pause".)
Break? How do I type it?Pressing Ctrl + C while a Python program is running will cause the Python interpreter to raise a KeyboardInterrupt exception.
It's likely that a program that makes lots of HTTP requests will have lots of exception handling code. If the except part of the try-except block doesn't specify which exceptions it should catch, it will catch all exceptions including the KeyboardInterrupt that you just caused. A properly coded Python program will make use of the Python exception hierarchy and only catch exceptions that are derived from Exception.
# This is the wrong way to do things
try:
# Some stuff might raise an I/O exception
except:
# Code that ignores errors
# This is the right way to do things
try:
# Some stuff might raise an I/O exception
except Exception:
# This won't catch KeyboardInterrupt
If you can't change the code (or need to kill the program so that your changes will take effect) then you can try pressing Ctrl + C rapidly. 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 raised when the program is outside of a try block.
If it is running in the Python shell, use Ctrl + Z. Otherwise, locate the python process and kill it.
^Z --> [1]+ Stopped --> kill %1 to stop job #1 (or job %1 as bash puts it)fg will bring it right back.Ctrl+Z is not the proper way to stop a python script and should be avoided unless you plan on resuming the paused process.Ctrl + C Difference for Windows and Linux
It turns out that as of Python 3.6, the Python interpreter handles Ctrl + C differently for Linux and Windows. For Linux, Ctrl + C would work mostly as expected. However, on Windows, Ctrl + C mostly doesn't work, especially if Python is running a blocking call, such as thread.join or waiting on a web response. It does work for time.sleep, however. Here's the nice explanation of what is going on in the Python interpreter. Note that Ctrl+C generates SIGINT.
Solution 1: Use Ctrl + Break or the equivalent
Use the below keyboard shortcuts in a terminal/console window which will generate a SIGBREAK at a lower level in the OS and terminate the Python interpreter.
macOS and Linux
Ctrl + Shift+\ or Ctrl + </kbd>
Windows (laptops):
Solution 2: Use the Windows API
Below are handy functions which will detect Windows and install a custom handler for Ctrl + C in the console:
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 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 the handler
install_handler()
# Now block
t.join()
# Ctrl + C works now!
Solution 3: Polling method
I don't prefer or recommend this method, because it unnecessarily consumes processor and power, negatively impacting the performance.
import threading
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 a KeyboardIntrupt exception
ctrl + c doesn't quit but showing KeyboardInterrupt.SetConsoleCtrlHandler(). You can just do a try: time.sleep(5);except KeyboardInterrupt: break; in a while 1 loop. This works with any long delay for event handlers, but should be used with care when using polling and using not less time than: time.sleep(0.01).The interrupt process is hardware- and OS-dependent. So you will have very different behavior depending on where you run your Python script. For example, on Windows machines, we have Ctrl + C (SIGINT) and Ctrl + Break (SIGBREAK).
So while SIGINT is present on all systems and can be handled and caught, the SIGBREAK signal is Windows-specific (and can be disabled in CONFIG.SYS) and is really handled by the BIOS as an interrupt vector INT 1Bh, which is why this key is much more powerful than any other.
So if you're using some Unix-like flavored OS, you will get different results depending on the implementation, since that signal is not present there, but others are. In Linux, you can check what signals are available to you by:
kill -l
Output:
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 the Ctrl + Break signal on a Linux system, you'll have to check to what POSIX signal they have mapped that key. Popular mappings are:
CTRL + \ = SIGQUIT
CTRL + D = SIGQUIT
CTRL + C = SIGINT
CTRL + Z = SIGTSTOP
CTRL + BREAK = SIGKILL or SIGTERM or SIGSTOP
In fact, many more functions are available under Linux, where the SysRq (System Request) key can take on a life of its own...
On Mac, press Ctrl+</kbd> to quit a Python process attached to a terminal.
On a Mac and in Terminal:
For the record, Ctrl+' killed the process on my Raspberry Pi 3B+ (running Raspbian). On my French AZERTY keyboard, the touch ' is also number 4.
You can open Task Manager (Ctrl + Alt + Delete, and then go to Task Manager) and look through it for Python and the server is called (for the example) _go_app (the naming convention is: _language_app).
If I end the _go_app task, it'll end the server, so going there in the browser will say it "unexpectedly ended". I also use Git Bash, and when I start a server, I cannot break out of the server in Bash's shell with Ctrl + C or Ctrl + Pause, but once you end the Python task (the one using 63.7 MB), it'll break out of the server script in Bash, and allow me to use the Git Bash shell. 
Those may help.
I faced the same issue this morning. On macOS, using pgrep Python to get the PID and then kill [pid] to kill the process worked fine.