进程不等待子进程

2024-05-23 22:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个在windows上处理外部可执行文件的项目。我正在使用Popen与外部可执行文件进行交互。你知道吗

工作流程如下:命令->;.exe->;输出文件->;读取输出文件

命令正在被馈送到.exe文件(如示例所示),但是,创建的进程没有等待命令执行,因此不会生成输出文件。我已经试过了时间。睡眠(1) 但无济于事。我注意到输出文件只在python代码终止时才被写入。你知道吗

for i in range(0, len(commands)):
    process.stdin.write(commands[i])
    #out, err=process.communicate()
    #print err

#process.terminate()
#Reading AVL output files

# Open AVLaero.dat

fid     = open('AVLaero.dat', 'r')
AVLaero=fid.read()
fid.close()
process.terminate()

当出现异常时,一旦程序终止,就会写入这些文件AVLaero.dat公司找不到。你知道吗

测试这是否是由avl.exe文件文件中,我添加了一个无限while循环,以便在给定avl.exe文件该写了。但是,这并没有导致输出文件被写入。你知道吗

我在虚拟机上运行Windows使用Parallels桌面。。。你知道吗


Tags: 文件项目命令gt可执行文件windowsprocessexe
2条回答
avl_path=os.getcwd()+'/avl.exe'
process=Popen(avl_path, stdin=PIPE, stdout=None, stderr=None)
for i in range(0, len(commands)):
    process.stdin.write(commands[i])
    #process.stdout.readinto(test)
    #out, err=process.communicate()
    #print err

#process.terminate()
#Reading AVL output files

# Open AVLaero.dat
#print test
process.wait()
fid     = open('AVLaero.dat', 'r')
AVLaero=fid.read()
fid.close()
process.terminate()

这工作正常!你知道吗

在继续python程序之前,您不会等待进程终止。你应该process.wait()让它完成。在与进程通信之后,或者使用注释中的process.communicate()(并自动调用wait())应该具有所需的功能。你知道吗

相关问题 更多 >