I created a bundle in TextMate for restarting the current Django project's associated Supervisor process. Running the code in the Python interpreter successfully restarts the process without blocking, however when I use it as a TextMate bundle (set to run every time I save .py file) it blocks the GUI for ~3 seconds. Is there any way I can avoid this?
Here's what the code looks like:
#!/usr/bin/env python
import os
import subprocess
import threading
projname = os.environ.get('TM_PROJECT_DIRECTORY', '').rpartition('/')[2]
def restart_proj(projname=None):
""" Restart a supervisor instance.
Assumes that the name of the supervisor instance is the basename for
TM_PROJECT_DIRECTORY.
"""
if projname:
subprocess.Popen('$HOME/.virtualenvs/supervisor/bin/' \
'supervisorctl restart {0}'.format(projname),
shell=True, stdout=open('/dev/null', 'w'))
t = threading.Thread(target=restart_proj, args=(projname, ))
t.start()
This is probably too late, but you would want to close it early with close_fds=True set in the Popen argument. With it specified, it does not wait for a response.
subprocess.Popen('$HOME/.virtualenvs/supervisor/bin/' \
'supervisorctl restart {0}'.format(projname),
shell=True, close_fds=True, stdout=open('/dev/null', 'w'))