脚本结束时不要终止python子进程

脚本结束时不要终止python子进程,python,subprocess,Python,Subprocess,我已经看到了很多与此相反的问题,我觉得很奇怪,因为我无法阻止我的子进程关闭,但是有没有办法调用subprocess.Popen并确保它的进程在调用python脚本退出后保持运行 我的代码如下: dname = os.path.dirname(os.path.abspath(__file__)) script = '{}/visualizerUI.py'.format(dname) self.proc = subprocess.Popen(['python', script, str(width)

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

我的代码如下:

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子流程,但我希望它保持打开状态。或者至少有选择权


我缺少什么?

删除stdout=subprocess.PIPE并添加shell=True,以便在可以分离的子shell中生成它。

另一个选项是使用:

import os
os.system("start python %s %s %s %s" % (script, str(width), str(height), str(pixelSize)))
使用新控制台在新进程中启动新的python脚本

编辑:刚刚看到你正在使用Mac电脑,所以是的,我怀疑这是否适合你

那么:

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

如果我添加shell=True,它在Mac上不起作用。没有标准部分,它也能做同样的事情。在Mac上不工作,我的意思是窗口不出现(Visualization rui.py运行Tkinter应用程序),它在shell=True上也能做同样的事情。对你有用吗?你试过把脚本放到后台吗?嗯??不要认为Windows有分叉我的目标是Mac,Windows和Linux。。。所以它需要在任何地方都能工作。显然,在Mac上,等效的命令是“open”,但我附近没有测试的命令。