Python:如何确定子流程子项是否已全部完成运行 [英] Python: How to determine subprocess children have all finished running

查看:153
本文介绍了Python:如何确定子流程子项是否已全部完成运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测安装程序何时从Python脚本中完成执行.具体来说,该应用程序是Oracle 10gR2数据库.目前,我在Popen中使用子流程模块.理想情况下,我将只使用wait()方法来等待安装完成执行,但是,文档中的命令实际上会生成子进程来处理实际安装.这是失败代码的一些示例代码:

I am trying to detect when an installation program finishes executing from within a Python script. Specifically, the application is the Oracle 10gR2 Database. Currently I am using the subprocess module with Popen. Ideally, I would simply use the wait() method to wait for the installation to finish executing, however, the documented command actually spawns child processes to handle the actual installation. Here is some sample code of the failing code:

import subprocess
OUI_DATABASE_10GR2_SUBPROCESS = ['sudo',
                                 '-u',
                                 'oracle',
                                 os.path.join(DATABASE_10GR2_TMP_PATH,
                                              'database',
                                              'runInstaller'),
                                 '-ignoreSysPrereqs',
                                 '-silent',
                                 '-noconfig',
                                 '-responseFile '+ORACLE_DATABASE_10GR2_SILENT_RESPONSE]
oracle_subprocess = subprocess.Popen(OUI_DATABASE_10GR2_SUBPROCESS)
oracle_subprocess.wait()

这里有一个类似的问题:杀死包含子进程的子进程从python 中获取,但所选答案不能解决子级问题,而是指示用户直接调用应用程序以等待.我正在寻找一种特定的解决方案,它将等待子流程的所有子流程.如果子进程数目未知怎么办?我将选择解决所有子进程等待完成的问题的答案.

There is a similar question here: Killing a subprocess including its children from python, but the selected answer does not address the children issue, instead it instructs the user to call directly the application to wait for. I am looking for a specific solution that will wait for all children of the subprocess. What if there are an unknown number of subprocesses? I will select the answer that addresses the issue of waiting for all children subprocesses to finish.

更清楚地了解故障:子进程在wait()命令之后继续执行,因为该命令仅等待顶级进程(在这种情况下为'sudo').这是此问题中已知子进程的简单图表: Python子进程模块-> Sudo-> runInstaller-> java->(未知)

More clarity on failure: The child processes continue executing after the wait() command since that command only waits for the top level process (in this case it is 'sudo'). Here is a simple diagram of the known child processes in this problem: Python subprocess module -> Sudo -> runInstaller -> java -> (unknown)

推荐答案

好的,这是一个技巧,仅在Unix下有效.它类似于此问题的答案之一:确保子进程已死在退出Python程序时.这个想法是创建一个新的过程组.然后,您可以等待该组中的所有进程终止.

Ok, here is a trick that will work only under Unix. It is similar to one of the answers to this question: Ensuring subprocesses are dead on exiting Python program. The idea is to create a new process group. You can then wait for all processes in the group to terminate.

pid = os.fork()
if pid == 0:
    os.setpgrp()
    oracle_subprocess = subprocess.Popen(OUI_DATABASE_10GR2_SUBPROCESS)
    oracle_subprocess.wait()
    os._exit(0)
else:
    os.waitpid(-pid)

我还没有测试过.它创建了一个额外的子流程来充当流程组的负责人,但要避免这种情况(我认为)要复杂得多.

I have not tested this. It creates an extra subprocess to be the leader of the process group, but avoiding that is (I think) quite a bit more complicated.

我发现此网页也有帮助. http://code.activestate.com/recipes /278731-creating-a-daemon-the-python-way/

I found this web page to be helpful as well. http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/

这篇关于Python:如何确定子流程子项是否已全部完成运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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