0

I'm a Python noob.

Is there a way to get the STDOUT and STDERR of all running processes in Python? Some processes exit after a long time and their status is constantly being written to STDOUT.

Elmo
  • 6,409
  • 16
  • 72
  • 140
  • do you mean literally all running proceses, or just processes you create while running your program? do you create multiple processes? Two major strategies are to create a thread for each process so that they run in parallel or to create files and redirect process output there. – tdelaney Sep 15 '14 at 19:37
  • Perhaps this answer helps you http://stackoverflow.com/a/18345099/841339 – Mauro Baraldi Sep 15 '14 at 19:41
  • Those are two *very* different questions. – chepner Sep 15 '14 at 20:26

1 Answers1

0

To get subprocess output line by line without waiting for it to close:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
while True:
    o = p.stdout.readline()
    if not o:
        break
    print o

you can use p.stdout.read(n) if want to read every 'n' characters from the process.

Hope this helps.