Search code examples
pythonpopen

process not waiting for children python


I'm working on a project where I deal with external executables on windows. I am using Popen to interact with the external executable.

The work flow is as below: commands -> .exe -> output_files -> read_outputfiles

The commands are being fed to .exe file as exepected however, the process created isn't waiting for the command to execute and hence the output files aren't generated. I've already tried time.sleep(1) but with no avail. I've noted that the output files are written only when the python code terminates.

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()

The files are written once the program terminates after an exception is raised when AVLaero.dat is not found.

To test if this is being caused by avl.exe file, I added an infinite while loop to just keep my python code running while giving avl.exe time to write. However, this didn't lead to the output files being written.

I'm running Windows on a virtual machine using Parallels Desktop...


Solution

  • You aren't waiting for the process to terminate before continuing your python program. You should process.wait() for it to finish. Adding that after your communication with the process, or using process.communicate(), which is in comments (and automatically calls wait()) should have the desired functionality.