Python.Popen subprocess hangs at 'for l in p.stdout' until p completes, why?

I have this code:

#!/usr/bin/python -u

localport = 9876

import sys, re, os
from subprocess import *

tun = Popen(["./newtunnel", "22", str(localport)], stdout=PIPE, stderr=STDOUT)

print "** Started tunnel, waiting to be ready ..."
for l in tun.stdout:
        sys.stdout.write(l)
        if re.search("Waiting for connection", l):
                print "** Ready for SSH !"
                break

      

"./newtunnel" will not exit, it will continually output more and more data to stdout. However, this code will not give any exit and just waits in tun.stdout.

When I kill the newtunnel process from the outside, it clears all data up to tun.stdout. It looks like I cannot get data from the tun.stdout file while it is still running.

Why? How can I get information?

Note that the default bufsize for Popen is 0 (unbuffered). I can also specify bufsize = 0, but that doesn't change anything.

+2


a source to share


1 answer


Ok, it seems like this is a bug in Python: http://bugs.python.org/issue3907

If I replace the line

for l in tun.stdout:

      



by

while True:
    l = tun.stdout.readline()

      

then it works exactly as i want.

+2


a source







All Articles