1

在我的python脚本中,我需要在for循环中调用一个可执行文件,并等待该可执行文件将结果写入“output.xml”。

我如何设法使用 wait() 以及我如何知道我的一个可执行文件何时完成生成结果以获得结果?如何关闭该进程并打开一个新进程以再次调用可执行文件并等待新结果?

    import subprocess
    args = ("bin/bar")
    popen = subprocess.Popen(args)

我需要等待“bin/bar”的输出来生成“output.xml”,然后从那里读取它的内容。

    for index, result in enumerate(results):
        myModule.callSubProcess(index)
        #this is where the problem is.
        fileOutput = open("output.xml")
        parseAndStoreInSQLiteFileOutput(index, file)
4

2 回答 2

1

Popen.wait()将使脚本等到进程结束。之后无需终止该进程,因为它已经退出了。

于 2010-03-20T17:43:07.277 回答
1

我认为最简单的方法是使用call

import subprocess
retcode = subprocess.call('command', shell=True)

它等待进程终止并将返回码分配给变量。有关更详细的描述,请参阅python 文档中的17.1.subprocess - 便利函数。希望能帮助到你。

于 2010-03-24T19:16:38.680 回答