2011-07-11 52 views
0

我在TextMate中创建了一个包,用于重新启动当前的Django项目的关联Supervisor进程。在Python解释器中运行代码可以成功重启进程而不会阻塞,但是当我将它作为TextMate包使用时(每次保存.py文件时都设置为运行),它会阻塞GUI约3秒。有什么办法可以避免这种情况吗?TextMate Python包非阻塞

下面的代码是什么样子:

#!/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() 

回答

0

这可能是太晚了,但你会想早点与在POPEN参数close_fds = true设置关闭。有了它的指定,它不会等待回应。

subprocess.Popen('$HOME/.virtualenvs/supervisor/bin/' \ 
        'supervisorctl restart {0}'.format(projname), 
        shell=True, close_fds=True, stdout=open('/dev/null', 'w')) 
+1

我不再使用这个包。但你的评估似乎是合理的! – jmagnusson