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
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)
Shital Shah Oct 23 '18 at 5:31 2018-10-23 05:31
source share