I was looking to implement a python script that called another script and captured its stdout. The called script will contain some input and output messages eg
print ("Line 1 of Text")
variable = raw_input("Input 1 :")
print "Line 2 of Text Input: ", vairable
The section of the code I’m running is
import subprocess
cmd='testfile.py'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
so, se = p.communicate()
print(so)
The problem that is occurring is that the stdout is not printing until after the script has been executed. This leaves a blank prompt waiting for the user input. Is there a way to get stdout to print while the called script is still running?
Thanks,
There are two problems here.
Firstly, python is buffering output to stdout and you need to prevent this. You could insert a call to
sys.stdout.flush()intestfile.pyas Ilia Frenkel has suggested, or you could usepython -uto executetestfile.pywith unbuffered I/O. (See the other stack overflow question that Ilia linked to.)You need a way of asynchronously reading data from the sub-process and then, when it is ready for input, printing the data you’ve read so that the prompt for the user appears. For this, it would be very helpful to have an asynchronous version of the subprocess module.
I downloaded the asynchronous subprocess and re-wrote your script to use it, along with using
python -uto get unbuffered I/O:When I run this script using
python -uI get the following results:and the script pauses, waiting for input. This is the desired result.
If I then type something (e.g. “Hullo”) I get the following: