Stopping python using ctrl+c

前端 未结 11 1439
-上瘾入骨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:03

    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 *nix 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
     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...

提交回复
热议问题