【发布时间】:2021-03-29 01:22:42
【问题描述】:
我有一个执行此操作的 python 脚本:
p = subprocess.Popen(pythonscript.py, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
theStdin=request.input.encode('utf-8')
(outputhere,errorshere) = p.communicate(input=theStdin)
它按预期工作,它通过 p.communicate() 等待子进程完成。但是在 pythonscript.py 中,我想“解雇并忘记”一个“孙子”进程。我目前正在通过覆盖 join 函数来做到这一点:
class EverLastingProcess(Process):
def join(self, *args, **kwargs):
pass # Overwrites join so that it doesn't block. Otherwise parent waits.
def __del__(self):
pass
然后像这样开始:
p = EverLastingProcess(target=nameOfMyFunction, args=(arg1, etc,), daemon=False)
p.start()
这也很好,我只是在 bash 终端或 bash 脚本中运行 pythonscript.py。当 EverLastingProcess 启动的子进程继续运行时,控制和响应返回。但是,当我运行 pythonscript.py 和 Popen 运行如上所示的进程时,从时间来看,Popen 正在等待孙子完成。 我怎样才能使 Popen 只等待子进程,而不是任何孙子进程?
【问题讨论】:
-
不会将 daemon=True 传递给 EverLastingProcess 帮助吗?
-
@rasjani 我使用 daemon=False 因为我希望子进程在父进程完成时不会死亡。它应该继续独立做自己的事情。
标签: python subprocess popen