I have a program which takes hours to complete and has to integrated into an existing procedure. I have a script which runs the program through the subprocess module and this works fine. But there is no way to tell how far the program has advances. The program does output some real-time information to stdout so I thought I could do something with reading from the pipes.
However I can`t get it to workycorrectly. It seems my script blocks on reading from the pipe in realtime.
I have made a simple script which demonstrates this:
import subprocess
worklist = [
{
'name' : '1: ',
'cmd' :r'/python27/python.exe printer.py',
'pid' :None
},{
'name' : '2: ',
'cmd' :r'/python27/python.exe printer.py',
'pid' :None
},{
'name' : '3: ',
'cmd' :r'/python27/python.exe printer.py',
'pid' :None
}
]
for work in worklist:
work['pid'] = subprocess.Popen(work['cmd'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,bufsize=0)
while True:
for work in worklist:
for line in work['pid'].stdout:
print work['name'] + str(line)
if all(item['pid'].poll() is not None for item in worklist):
break
for work in worklist:
work['pid'].communicate
the printer.py contains:
from time import sleep
print 'this is a process line 1'
sleep(1)
print 'this is a process line 2'
sleep(1)
print 'this is a process line 3'
sleep(1)
print 'this is a process line 4'
sleep(1)
print 'this is a process line 5'
sleep(1)
print 'this is a process line 6'
sleep(1)
print 'this is a process line 7'
sleep(1)
print 'this is a process line 8'
sleep(1)
print 'this is a process line 9'
sleep(1)
so What I would expect to see is a that it prints the output of every 'printer.py' I called in real-time. However it just prints it out all at the same time once it has finished the process.
Is there some way around this by using only the subprocess module or other python build-ins?
I am on a windows machine so I can't use pexpect, plus since this will have to be used on a lot of systems I don't want to introduce dependencys... or at least as little as possible