How to iterate over two lists using iter () and yield?

A simple example:

 popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True)
 for stdout_line in iter(popen.stdout.readline, ""): # how to add popen.stderr.readline check?
     yield stdout_line

We read from popen.stdout, but we also want to read from stderrat the same time! We do not know when the process will end.

So how to iterate over two lists using iter () and yield?

+4
source share
1 answer

These are not lists, and the right way to work with them is not how you work with lists. If you want to write the stdout and stderr process into one combined stream, do this with output redirection:

from subprocess import PIPE, STDOUT

process = subprocess.Popen(cmd, stdout=PIPE, stderr=STDOUT, ...)
#                                                   ^^^^^^
+4
source

Source: https://habr.com/ru/post/1693274/


All Articles