TextMate Python捆绑包非阻塞

2024-03-28 17:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在TextMate中创建了一个bundle,用于重新启动当前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()

Tags: the代码importprojectbinosdirectorytm
1条回答
网友
1楼 · 发布于 2024-03-28 17:42:54

这可能太晚了,但您可能希望在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'))

相关问题 更多 >