我在 TextMate 中创建了一个包,用于重新启动当前 Django 项目的相关主管进程。在 Python 解释器中运行代码成功地重新启动进程而不会阻塞,但是当我将它用作 TextMate 包(设置为每次保存 .py 文件时运行)时,它会阻塞 GUI 约 3 秒。有什么办法可以避免这种情况吗?
代码如下所示:
#!/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()