Can't kill a python process in sublime text 2

后端 未结 9 1017
难免孤独
难免孤独 2021-02-04 14:06

I\'ve searched in a lot of places but I can\'t seem to get the keywords correct. I have a stalling process in Python in Sublime that causes the beachball of death on a Mac. I ca

相关标签:
9条回答
  • 2021-02-04 14:10

    If you are a mac or linux user, you can kill all python processes by killall command.

    killall -9 Python
    

    Quick Access

    You may later add as an alias to the bash_profile script and run the same code by calling killpython.

    nano ~/.bash_profile
    

    Add this line as alias.

    alias killpython='killall -9 Python; echo "killed all snakes                                                                     
    0 讨论(0)
  • 2021-02-04 14:14

    I found an interesting way to solve this.

    My build system on sublime-text2 call my Makefile which has 2 options and uses the ONESHELL directive:

    .ONESHELL:
    run: myprogram
        ./myprogram &
        echo $$! > "pid.tmp"
    

    Note that after it starts my program its pid is saved on a file. And i have a second option:

    stop:
        kill -9 `cat pid.tmp`
        rm pid.tmp
    

    This option kills the process started by the run command.

    Then i configured my build system (Tools -> Build System -> New Build System...) so i could access both options:

    {
        "cmd": ["make"],
        "variants":
        [
                {
                        "name": "Run",
                        "cmd": ["make", "run"]
                },
                {
                        "name": "Stop",
                        "cmd": ["make", "stop"]
                }
        ]
    

    }

    But i want to call both options from key bindings on sublime so i edited the file "Preferences -> Key Bindings - User" to look like this:

    [
    { "keys": ["ctrl+r"], "command": "build", "args": {"variant": "Run"} },
    { "keys": ["alt+r"], "command": "build", "args": {"variant": "Stop"} }
    ]
    

    Now if i press ctrl+r my program starts (and enters an infinity loop) and when i press alt+r my program stops which is almost what i wanted.

    The only problem left is that when i run alt+r i loose the output produced by ctrl+r.

    Edit: Other way i found was to start my program on a new xterm process on the Makefile:

    run:
        xterm ./myprogram
    

    I can close it with ctrl+c and it wont stop sublime from working.

    0 讨论(0)
  • 2021-02-04 14:17

    I have not found a way to kill a process in Sublime without killing all of Sublime. But I did find a way to make killing and reopening Sublime much less painful. Here is my way.

    1. Command-option-escape: This brings up the force-quit window.
    2. The force-quit window will ask you if you really want to quit Sublime twice. Just hit enter twice.
    3. A crash reporter window will open. I hated this window, because it floats on top of everything so you can't ignore it, and closing it made me do extra keystrokes, so I disabled it.
    4. Reopen sublime. When Sublime re-opens, it opens all of the files you had open before it crashed, taking you back to where you were.

    Many people think reopening Sublime is pain in the butt because they have to navigate to the Applications Directory with their mouse, a process which takes about 10 - 30 seconds. I used to find this annoying, so I set it up so that I could reopen sublime with five keystrokes, and it takes me about three seconds.

    First, I installed Alfred. In this case, you only need the free version of Alfred.

    With Alfred installed, do the following:

    1. option-spacebar brings up the Alfred search bar. Alfred is a lot like Google Search for your computer.
    2. Type the letters "su." Below the Alfred search bar, a bunch of options appear, and the first option is Sublime Text 2. Alfred automatically highlights the first thing on it's list of search results, so that when you hit enter, it will launch the highlighted application.
    3. Hit enter. Alfred opens Sublime. Voila: Sublime is back to how it was before you ran the script.

    So, in total, once I start a process that freezes Sublime, I do the following 10 keystrokes:

    Cmd-option-escape enter enter option-spacebar s u enter

    This procedure does leave the Force Quit Applications window hanging around, because I have not found a quick way to get rid of it without adding ten extra keystrokes to my system. If it really bugs me, I click on the window and do cmd-w, which closes the window.

    The other annoying thing is that it takes a couple of seconds for Sublime to relaunch, so usually I don't bother to run things from within Sublime. Instead, I go over to the terminal and run things there, so that I can Ctrl-C the script I'm testing without affecting Sublime.

    Additionally, there is a keyboard shortcut for the Tools > Cancel Build option. I have never used it, but using it and fixing problems with it is discussed in this forum post.

    0 讨论(0)
  • 2021-02-04 14:20

    I have "ported" VGarcia's solution to Python. However, if you need a Makefile for your project anyway, you should probably go for the Makefile method.

    Remark on VGarcia's solution:

    • VGarcia's solution worked nice for me, but I had to write ./myprogram & echo $$! > "pid.tmp" on a single line, otherwise the stored PID would end being empty.

    • I wanted to build the binary before running so I just added a build target myprogram and kept the dependency run: myprogram

    (I know this should be a comment, but I did not have enough reputation for that. I will make it a comment when I can.)

    Python version

    Write a run.py script that runs the program and stores the PID of the process (see https://docs.python.org/2/library/subprocess.html#subprocess.Popen):

    from subprocess import Popen  # prefer subprocess32 in Python 2 for Linux / OS X
    
    # run process as child (still receive process output)
    pid = Popen(['build/myprogram']).pid
    
    # store pid
    with open('pid.tmp', 'w') as f:
        f.write(str(pid))
    

    Write a stop.py script to read the PID and kill the process:

    import os, signal
    from subprocess import Popen
    
    # read PID
    with open('pid.tmp', 'r') as f:
        pid = f.readline().rstrip()
    
    # kill process
    os.kill(int(pid), signal.SIGKILL)
    
    # remove temp file
    os.remove('pid.tmp')
    

    Configure the build:

    {
        "name": "Python Build",
        // adapt for your project
        "cmd": [my_build_cmd],
        "working_dir": "$project_path",
        "variants":
        [
            {
                "name": "Run",
                "cmd": ["python", "run.py"], 
            },
            {
                 "name": "Stop",
                 "cmd": ["python", "stop.py"],
            }
        ]
    }
    

    Or you can put all the functions in one module and call the different methods runand stop by parsing some arguments in main().

    Improvement suggestion 1 (not tried myself)

    You may want to reuse your scripts for different projects. In this case:

    1. Change your scripts into functions with parameters. You will probably want at least def run(bin_path). Put the script(s) somewhere you can easily access from any project.

    2. In your sublime-project, put the build configuration in the build_systems entry (the value is a list, see https://www.sublimetext.com/docs/2/projects.html, a bit old but usable). You can either call the functions directly with "cmd": ["python", "-c", "from script import run; run(bin_path)"] or use main() as suggested above.

    Improvement suggestion 2 (not tried myself)

    If you run the program through multiple processes at once (accidentally, or to test some networking feature), the stop procedure will only kill the last process. You can adapt the algorithm to store multiple PIDs in the temp file, then kill each process accordingly.

    You can also do this with the Makefile method as a shell script. In any case, be sure to pick a language you feel at ease with.

    0 讨论(0)
  • 2021-02-04 14:25

    None of the answers on this page actually worked for me.
    The sad truth seems to be that there are no "civilized" way of killing a python script started on sublime (CTRL/COMMAND + B).

    0 讨论(0)
  • 2021-02-04 14:26

    Launch task manager or whatever process manager you have on your computer (if you don't, get one). Delete the corresponding python.exe and pythonw.exe process

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