脚本结束时不要终止 python 子进程 [英] Do NOT terminate python subprocess when script ends

查看:33
本文介绍了脚本结束时不要终止 python 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多与此相反的问题,我觉得很奇怪,因为我无法阻止我的子进程关闭,但是有没有办法调用 subprocess.Popen 并确保在调用 python 脚本退出后它的进程保持运行?

I've seen a ton of questions for the opposite of this which I find odd because I can't keep my subprocess from closing but is there a way to call subprocess.Popen and make sure that it's process stays running after the calling python script exits?

我的代码如下:

dname = os.path.dirname(os.path.abspath(__file__))
script = '{}/visualizerUI.py'.format(dname)
self.proc = subprocess.Popen(['python', script, str(width), str(height), str(pixelSize)], stdout=subprocess.PIPE)

这很好地打开了进程,但是当我关闭我的脚本时(因为它完成了或者使用了 Ctrl+C)它也会关闭visualizerUI.py 子进程,但我希望它保持打开状态.或者至少有选择.

This opens the process just fine, but when I close out of my script (either because it completes or with Ctrl+C) it also closes the visualizerUI.py subprocess, but I want it to stay open. Or at least have the option.

我错过了什么?

推荐答案

另一种选择是使用:

import os
os.system("start python %s %s %s %s" % (script, str(width), str(height), str(pixelSize)))

使用新控制台在新进程中启动新的 python 脚本.

To start your new python script in a new process with a new console.

刚刚看到您在 Mac 上工作,所以是的,我怀疑这对您有用.

just saw that you are working on a Mac, so yeah I doubt this will work for you.

怎么样:

import os
import platform

operating_system = platform.system().lower()
if "windows" in operating_system:
    exe_string = "start python"
elif "darwin" in operating_system:
    exe_string = "open python"
else:
    exe_string = "python"
os.system("%s %s %s %s %s" % (exe_string, script, str(width),
          str(height), str(pixelSize))))

这篇关于脚本结束时不要终止 python 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆