Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: why print statements and subprocess.call() output are out of sync?

I am running the following piece of code (call it batch.py)

for config in keystoneConfig: 
    cmdlist = generate_cmd_list(config)
    print ' '.join(cmdlist)
    subprocess.call(cmdlist)

And redirecting the output of batch.py to another file. i.e.

./batch.py > output.txt

But I realize that all the output from subprocess.call() goes before the print statement. Why is the output out of sync?

like image 548
CuriousMind Avatar asked Apr 20 '12 21:04

CuriousMind


People also ask

How do I capture the output of a subprocess run?

To capture the output of the subprocess. run method, use an additional argument named “capture_output=True”. You can individually access stdout and stderr values by using “output. stdout” and “output.

Does subprocess wait for command to finish?

subprocess. run() is synchronous which means that the system will wait till it finishes before moving on to the next command. subprocess. Popen() does the same thing but it is asynchronous (the system will not wait for it to finish).

How do you check subprocess output?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.

What is the difference between subprocess call and Popen?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.


2 Answers

Python is block buffering its own output and not flushing it before subprocess.call(), because you redirected its output to a file instead of the console; you would need to force line buffering or disable buffering, or manually flush before the subprocess call.

like image 106
geekosaur Avatar answered Oct 25 '22 20:10

geekosaur


Flush with sys.stdout.flush() after printing and before making the subprocess call.

like image 36
db 1070 Avatar answered Oct 25 '22 21:10

db 1070