I am trying to make a simple python script that starts a subprocess and monitors its standard output. Here is a snippet from the code:
JavaScript
x
5
1
process = subprocess.Popen([path_to_exe, os.path.join(temp_dir,temp_file)], stdout=subprocess.PIPE)
2
while True:
3
output=process.stdout.readline()
4
print "test"
5
The problem is that the script hangs on output=process.stdout.readline()
and that the line print "test"
only executes after the subprocess is terminated.
Is there a way to read standard output and print it without having to wait for the subprocess to terminate?
The subprocess which I am starting is a Windows binary for which I do not have the source code.
I have found several similar questions, but the answers are only applicable on Linux or in case I have the source of the suprocess I am starting.
Advertisement
Answer
Check select module
JavaScript
1
16
16
1
import subprocess
2
import select
3
import time
4
5
x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)
6
7
y=select.poll()
8
y.register(x.stdout,select.POLLIN)
9
10
while True:
11
if y.poll(1):
12
print x.stdout.readline()
13
else:
14
print "nothing here"
15
time.sleep(1)
16
EDIT:
Threaded Solution for non posix systems:
JavaScript
1
26
26
1
import subprocess
2
from threading import Thread
3
import time
4
5
linebuffer=[]
6
x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)
7
8
def reader(f,buffer):
9
while True:
10
line=f.readline()
11
if line:
12
buffer.append(line)
13
else:
14
break
15
16
t=Thread(target=reader,args=(x.stdout,linebuffer))
17
t.daemon=True
18
t.start()
19
20
while True:
21
if linebuffer:
22
print linebuffer.pop(0)
23
else:
24
print "nothing here"
25
time.sleep(1)
26