简体   繁体   中英

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. ie

./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?

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.

在打印之后和进行子进程调用之前使用sys.stdout.flush()进行刷新。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM