Python subprocess.Popen hangs in 'for l in p.stdout' until p terminates, why?
Posted
by Albert
on Stack Overflow
See other posts from Stack Overflow
or by Albert
Published on 2010-05-14T22:28:19Z
Indexed on
2010/05/14
22:44 UTC
Read the original article
Hit count: 213
I have that 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
The "./newtunnel" will not exit, it will constantly output more and more data to stdout. However, that code will not give any output and just keeps waiting in the tun.stdout.
When I kill the newtunnel process externally, it flushes all the data to tun.stdout. So it seems that I can't get any data from the tun.stdout while it is still running.
Why is that? How can I get the information?
Note that the default bufsize for Popen is 0 (unbuffered). I can also specify bufsize=0 but that doesn't change anything.
© Stack Overflow or respective owner