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

时间:2014-08-04 21:07:40

标签: python subprocess

我已经看到一个 ton 的问题与此相反,我觉得很奇怪,因为我不能让我的子进程关闭,但是有办法调用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子进程,但我希望它保持打开状态。或者至少可以选择。

我错过了什么?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

另一种选择是使用:

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))))
相关问题