-1

I am currently calling an external c++ program which generates a lot output by using subprocess.check_output, and save the stdout into a log file. However c++ program may take very long time to finish, but I want to view the stdout before it finished, just like if I directly execute the c++ program via bash command line.

Of course, I may accomplish this by directly modifying my c++ program using fstream to save the output. But I wonder if there's any solution in python which can intercept the stdout.

Thank you in advance!

jyhd
  • 11
  • Do you know the `tee` command ? – hivert Jul 23 '13 at 08:17
  • @hivert Of course I can just add 'tee' into the command of calling c++ program. But my question is about how to accomplish that in Python. @Frodon answered the question. It turns out I was so used to use `subprocess.check_output` when I come to deal with outputs, but was totally not aware of the `stdout=logfile` for `subprocess.call`. – jyhd Aug 03 '13 at 11:46

1 Answers1

1

If you save the output into a log file, why don't you use subprocess.call with stdout set to the opened log file ?

with open("logfile") as logfile:
  subprocess.call("run_my_program arg1 arg2", shell=True, stdout=logfile)

Then a simple tail -f logfile will show you the content of the log during the execution of the program.

Frodon
  • 3,309
  • 1
  • 12
  • 31
  • Thanks @Frodon That works! Didn't know that `stdout` is used internally in `subprocess.check_output` but not others. – jyhd Jul 23 '13 at 09:57
  • +1 for `stdout=logfile`. Note: the C++ might use a block buffering instead of a line buffering if stdout is not a tty i.e., the output won't be in "real time" ([follow the links for possible workarounds](http://stackoverflow.com/a/17698359/4279)). Don't use `shell=True` unnecessarily, try the list argument instead: `["my_program", "arg 1", "arg 2"]`. Open the file in binary mode ("wb") for readability. – jfs Jul 23 '13 at 13:14
  • @J.F.Sebastian Thanks for the C++ point. Just for reference, I also found the comment by Daniel Gallagher in [this answer](http://stackoverflow.com/a/4752069/1863827) explaining the point very clearly. If stdout is tty, `\n` will flush the output buffer, otherwise not, while `std::endl` will always flush the output buffer. The difference between `std::endl` and `\n` is explained very well by [this answer](http://stackoverflow.com/a/213977/1863827). – jyhd Jul 23 '13 at 21:54