Home > Software engineering >  Python subprocess call is not running in order
Python subprocess call is not running in order

Time:08-19

I am attempting to write a python file that calls a bash script. I have the following code

Print ("hello before subprocess call")
subprocess.call(['/builds/adummyscript.sh'], shell=True)
Print ("hello after subprocess call")

The dummy script is just a bash script with the following code

echo "hello from the inside of the subprocess call"

the output of my code is then

hello from the inside of the subprocess call
hello before subprocess call
hello after subprocess call

My question is why is the subprocess being run before the print statement and how can i fix this issue?

CodePudding user response:

The program is running after your print statement, python just doesn't flush its stdout buffer before the subprocess is called. use flush=True to flush python stdout or use unbuffered python when calling your script.

print("hello before subprocess call",flush=True)
  • Related