The Troll's factory

Geekeries & pensées

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.

Share and Enjoy:
  • Print
  • PDF
  • Twitter
  • Facebook
  • LinkedIn
  • RSS
  • Wikio FR
  • Digg
  • Google Bookmarks
  • Technorati
  • Sphinn
  • Mixx
  • Add to favorites
  • Live
  • Netvibes
  • Scoopeo
  • viadeo FR
  • MySpace
  • StumbleUpon
  • Yahoo! Buzz
posté par Troll dans astuces,Python,Scripts, astuces, dév. web avec 1 commentaire

Une réponse à “Output Popen subprocess stdout (and stderr) in real-time in Python”

  1. Nice tip, we’re looking to use this to help ease our process here.

Poster un commentaire

Remplissez le formulaire suivant pour poster un message.
Site internet
Votre commentaire