Stopping python using ctrl+c

前端 未结 11 1413
-上瘾入骨i
-上瘾入骨i 2020-11-29 19:44

I have a python script that uses threads and makes lots of HTTP requests. I think what\'s happening is that while a HTTP request (using urllib2) is reading, it\'s blocking a

相关标签:
11条回答
  • 2020-11-29 20:16

    Pressing Ctrl + c while a python program is running will cause python 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 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 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.

    0 讨论(0)
  • 2020-11-29 20:16

    You can open your task manager (ctrl + alt + delete, then go to task manager) and look through it for python and the server is called (for the example) _go_app (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.

    0 讨论(0)
  • 2020-11-29 20:23

    This post is old but I recently ran into the same problem of Ctrl+C not terminating Python scripts on Linux. I used Ctrl+\ (SIGQUIT).

    0 讨论(0)
  • 2020-11-29 20:23

    On a mac / in Terminal:

    1. Show Inspector (right click within the terminal window or Shell >Show Inspector)
    2. click the Settings icon above "running processes"
    3. choose from the list of options under "Signal Process Group" (Kill, terminate, interrupt, etc).
    0 讨论(0)
  • 2020-11-29 20:24
    • Forcing the program to close using Alt+F4 (shuts down current program)
    • Spamming the X button on CMD for e.x.
    • Taskmanager (first Windows+R and then "taskmgr") and then end the task.

    Those may help.

    0 讨论(0)
提交回复
热议问题