0

My problem is very similar to the problems described here and here with one thing not being covered: Assume, that I have an external longrunning.exe on windows, and I'm calling that with subprocess.Popen(). My exe now prints some commands and after some time, it gets into a loopback-mode and waits for input. When that happens, it outputs a single dot every second in the windows command prompt. It does put the subsequent dots on the same line, whereas all output before that is on its own line. I seem to be able to catch all output before that, but I cannot get this output, probably to some buffering going on(?). How can I get this output in my python console? Relevant code as below:

import subprocess
import sys

cmd = 'long_running.exe'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, b''):
    sys.stdout.write(line)
Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • Try setting `bufsize=0` and read by character instead of by line. If this doesn't work, then long_running.exe is using either line buffering or full buffering (typically 4 KiB) with manual and automatic flushing. I'd expect the latter when stdout is a pipe. If the program doesn't have a command-line option to disable buffering, there's nothing simple you can do about it. But why do you need this sequence of dots? They're just to let an interactive user know that the program isn't hung. – Eryk Sun Sep 12 '19 at 13:10
  • 1
    Don't use `shell=True` to run an executable. That option exists for running shell commands, which in Windows means cmd.exe internal commands such as `dir` and `type`. – Eryk Sun Sep 12 '19 at 13:14
  • @ErykSun isn't bufsize=0 the default? – Dschoni Sep 12 '19 at 13:35
  • 1
    You must be using Python 2. (FYI, its end of life is 2020-01; time to upgrade). In Python 3, the default is `bufsize=-1`, which uses default buffering. – Eryk Sun Sep 12 '19 at 14:28
  • Thx. I was looking at old docs. Stupid me. It works now. – Dschoni Sep 12 '19 at 14:41

0 Answers0