Realtime Output from Subprocess
When executing command line programs using Python's subprocess module, developers often encounter the issue of buffered output. This can be problematic for tasks that require line-by-line processing.
To resolve this issue, users can consider the following approach:
while True: line = p.stdout.readline() if not line: break # No more output, exit loop # Process the current line as needed...
This method allows for real-time output processing without encountering buffering issues. Despite initial attempts with for line in p.stdout: and bufsize settings, this approach has been proven to effectively capture output in a timely manner.
以上是如何在 Python 中获取子进程的实时输出?的详细内容。更多信息请关注PHP中文网其他相关文章!