-1

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.

Suresh
  • 925
  • 1
  • 9
  • 23

1 Answers1

1

use subprocess:

import subprocess

with open(logFileName, 'w') as fp:
    p = subprocess.Popen( [myexec], stdout=subprocess.PIPE, stderr=subprocess.PIPE )
    fp, error_msg = p.communicate()
Nemelis
  • 4,858
  • 2
  • 17
  • 37
  • I don't want to block until the process terminates. Do I need to look into threads? – Suresh Jan 25 '18 at 13:48
  • See the answer on my question in https://stackoverflow.com/questions/34314475/how-to-make-subprocess-only-communicate-error – Nemelis Jan 25 '18 at 14:36