1

I want to make a python program who monitor DumpIt RAM dump. Whatever, figure out it's just a command line program who ask a Y/N question before do his job and exit.

I tried to do this with Python subprocess module but the read of stdout is blocking. I think because buffering of standard output.

When I launch a program who print something then exit, it works, but I want to get the live output while the process is running.

I have already tried the following:

live output from subprocess command

http://www.saltycrane.com/blog/2009/10/how-capture-stdout-in-real-time-python/

Here is the code on Windows 10 x64, Python 3.5

print("start dump")
proc = subprocess.Popen([os.getcwd() + "\Ressources\Dumper\DumpIt.exe"], bufsize=0, stdout=subprocess.PIPE)

print("dump launched")

#v1
for line in iter(proc.stdout.readline, ''):
    sys.stdout.write(str(line))

#v2
# while True:
#     line = proc.stdout.readline()
#     sys.stdout.write(str(line))
#     if line == "" and proc.poll() is not None:
#         break

print("wait")
proc.wait()
print("done")

Output :

start dump
dump launched

sorry for bad English

Community
  • 1
  • 1
Jidey
  • 299
  • 2
  • 5
  • 20
  • If the command expects input (Y/N) and you don't provide it then it may hang indefinitely. If the output is in the **internal** `DumpIt.exe` stdout buffer then it is pointless to set `bufsize=0` that regulates **external** buffer in your parent Python script. btw, why do you need to capture the output if all you do is printing it back? Unrelated: 1- drop `iter()` 2- drop `str()` See [here's how Python 3 code, to live output should look like](http://stackoverflow.com/a/17698359/4279) – jfs Apr 30 '16 at 16:48
  • @J.F.Sebastian, I need to capture It because it does not print by itself. Let me know if you have another way to print subprocess output. – Jidey May 02 '16 at 08:17
  • `subprocess.check_call('echo it is printed by default', shell=True)` – jfs May 02 '16 at 08:32
  • `print("start dump")` `proc = subprocess.check_call([os.getcwd() + "\Ressources\Dumper\DumpIt.exe"], shell=True)` `print("dump launched")` Only print "start dump" and nothing else – Jidey May 02 '16 at 08:46
  • what happens if you run the command that I've provided? Run it in the Windows console. – jfs May 02 '16 at 09:05
  • It works but I think it is because the process die after printing. Does not work with DumpIt ... Here is the link to download it : http://www.moonsols.com/wp-content/plugins/download-monitor/download.php?id=7 – Jidey May 02 '16 at 12:37

0 Answers0