TextMate Python 捆绑包非阻塞

发布于 2024-11-19 04:24:21 字数 858 浏览 4 评论 0原文

我在 TextMate 中创建了一个包,用于重新启动当前 Django 项目的关联 Supervisor 进程。在 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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-11-26 04:24:22

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

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'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文