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
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 run
and 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:
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.
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.