TextMate Python 捆绑包非阻塞
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能已经太晚了,但您可能希望通过在 Popen 参数中设置 close_fds=True 来提前关闭它。指定后,它不会等待响应。
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.