简体   繁体   English

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

[英]Do NOT terminate python subprocess when script ends

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

My code is as follows:我的代码如下:

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)

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.这可以很好地打开该过程,但是当我关闭脚本时(因为它完成或使用 Ctrl+C),它也会关闭 VisualizerUI.py 子进程,但我希望它保持打开状态。 Or at least have the option.或者至少可以选择。

What am I missing?我错过了什么?

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

"

Another option would be to use:另一种选择是使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM