Please help with feeding a popen2 pipe with a large input

Alain TESIO alain at onesite.org
Wed Jan 10 13:06:28 EST 2001


Carsten Gaebler  wrote

> This is because you never read the output of the pipe,
> so cat's STDOUT fills up and blocks which keeps cat
> from reading from STDIN which fills up and blocks, too.

It hangs when I'm trying to read from stdout :

total length is 249866, bufsize is 1000
writing from 0 to 999 ...ok
Reading out ...


It works fine with a limited output from wc instead of cat, when I'm reading stdout and
stderr after stdin in closed.

What's the difference between os.popen3 and popen2.popen3, except the parameter order is
not the same ?

Alain



Here's the script attempting to read stdout :


def MyPipe(command,string_in):
	bufsize=1000
	import popen2
	(pipe_out,pipe_in,pipe_err)=popen2.popen3(command,bufsize*2)
#	(pipe_in,pipe_out,pipe_err)=os.popen3(command,bufsize*2)
	l=len(string_in)
	sys.stdout.write("total length is %i, bufsize is %i\n" % (l,bufsize))
	start=0
	end=0
	string_out=""
	string_err=""
	while end<l:
		end=min(l,start+bufsize)
		sys.stdout.write("writing from %i to %i ..." % (start,end-1))
		pipe_in.write(string_in[start:end])
		sys.stdout.write("ok\n")
		start=start+bufsize
		sys.stdout.write("Reading out ...")
		string_out=string_out+pipe_out.read()
		sys.stdout.write("ok\n")
		sys.stdout.write("Reading err ...")
		string_err=string_err+pipe_err.read()
		sys.stdout.write("ok\n")
	sys.stdout.write("closing pipe stdin ...")
	pipe_in.close()
	sys.stdout.write("ok\n")
	pipe_err.close()
	pipe_out.close()
	sys.stdout.write("out : %i bytes, err : %i bytes\n" %
(len(string_out),len(string_err)))
	return (string_out,string_err)

f=open("tmp")
h=f.read()
f.close()

execfile("observer.py")
(out,err)=MyPipe("cat",h)







More information about the Python-list mailing list