The Troll's factory

Geekeries & pensées

Archive pour la catégorie "Python"

Output Popen subprocess stdout (and stderr) in real-time in Python

So you’re writing a Python script and you are launching some other external command.

To achieve that, you are using the subprocess.Popen command, right.

But the subprocess is very slow, and it outputs data during it runtime, so you would like to output this data to the user as well, so that the user does not wait for the end of the subprocess before getting the information?

Example of a slow subprocess:

for i in 1 2 3 4 5 6; do sleep 5 && echo "${i}th output" && echo "err output num ${i}" >&2; done

Well, that’s fairly easy, just redirect the subprocess’s stdout to your own stdout:

import sys
from subprocess import Popen
Popen("./", stdout=sys.stdout, stderr=sys.stderr).communicate()

Although the catch here is the .communicate(). This will allow your program to wait for the end of the subprocess before executing the next line of code. So that your programm does not terminate execution before its subprocess, for instance.

Not that if you want to deamonize the script / zombify it, you can remove the .communicate() and the redirection of stdout and stderr will still work after your program’s termination.

posté par Troll dans astuces,Python,Scripts, astuces, dév. web avec 1 commentaire