Home >Backend Development >Python Tutorial >How to Stream Subprocess Output in Real-time using Python's `subprocess.communicate()`?
Streaming Output from subprocess.communicate()
In Python, using subprocess.communicate() to read stdout from a process that runs for an extended period can be problematic. The resulting output is received all at once, hindering real-time display.
To overcome this, it's possible to print each line of stdout as it's generated while still ensuring the process termination before proceeding:
Python 2
#!/usr/bin/env python2 from subprocess import Popen, PIPE p = Popen(["cmd", "arg1"], stdout=PIPE, bufsize=1) with p.stdout: for line in iter(p.stdout.readline, b''): print(line) p.wait() # Wait for subprocess termination
Python 3
#!/usr/bin/env python3 from subprocess import Popen, PIPE with Popen(["cmd", "arg1"], stdout=PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: print(line, end='')
This modified approach continuously reads lines as they are written, bypassing the buffer delay. The process still retains its blocking behavior, halting execution until termination.
The above is the detailed content of How to Stream Subprocess Output in Real-time using Python's `subprocess.communicate()`?. For more information, please follow other related articles on the PHP Chinese website!