2011-07-11 50 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