In my script, I redirect stdout to a file as below:
with open(logFileName, 'w') as fp:
proc = Popen([myexe], stdout=fp)
# proc.wait() # I don't want to block until process completes.
I understand that the fp would be closed even before the process completes. Thus my program does not work as expected.
If I do a wait(), it will work but I don't want to block.
I am wondering what is the right way to do this. Is a separate thread the only way? Surprisingly, I could not find an answer through google through this requirement should be a very common one.
Update: I see that it was not working for a different reason. It works fine even though the file object would be closed even before the process completes. Still not sure if this is right way to do.