Problems with select

Alain Tesio alain at onesite.org
Tue Feb 26 04:17:30 CET 2002


Hi, I can't manage to make select work, in both 1.52 and 2.1.2

I just want to read stderr and stdout when executing a command
with popen3, and I must use select, read() blocks when there is
more than a fixed amount of data (4 KB I think)

The problems is that select still gives something after the end
of the file, or sometimes never returns anything at all.

I create the pipes with :

(pipe_out,pipe_in,pipe_err)=popen2.popen3(command)

and use select like this:

pollstatus=select.select([],[pipe_out],[pipe_err],1)


(the doc says : select(iwtd, owtd, ewtd[, timeout])
I didn't put pipe_in as it's closed)


Here is my test program:

========================
#!/usr/bin/python

import sys,select,popen2,string

def execute(command,maxiter):
	(pipe_out,pipe_in,pipe_err)=popen2.popen3(command)
	pipe_in.close()
	pipes=[pipe_out,pipe_err]
	gotsomething=1
	i=0
	while gotsomething:
		gotsomething=0
		i=i+1
		if i>maxiter:
			print "maxiter reached"
			sys.exit(0)
		pollstatus=select.select([],[pipe_out],[pipe_err],1)
		print "select returns : ",pollstatus
		for (pollitem,label,pipe) in ((1,"out",pipe_out),
									  (2,"err",pipe_err)):
			if pollstatus[pollitem]:
				print "%i on %s : '%s'" % (i,label,string.strip(pipe.readline()))
				gotsomething=1

execute("cat 3lines",10)
========================

And the ouput continues after the file is exhausted (readline doesn't
fail), same problem with no timeout.

04:09:32 alain ~/tmp $wc -l 3lines
      3 3lines
04:09:32 alain ~/tmp $./test_select.py
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
1 on out : 'first'
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
2 on out : 'second'
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
3 on out : 'third'
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
4 on out : ''
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
5 on out : ''
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
6 on out : ''
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
7 on out : ''
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
8 on out : ''
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
9 on out : ''
select returns :  ([], [<open file '(fdopen)', mode 'r' at 0x8060480>], [])
10 on out : ''
maxiter reached


And sometimes (really randomly, about 1 time out of 10) it just gives
nothing, with no timeout it hangs forever.

04:11:28 alain ~/tmp $./test_select.py
select returns :  ([], [], [])


What did I miss ?
I've succeeded with select.poll but I must use select.select to have my
program usable with python 1.52 (still in Debian stable)


Thanks
Alain



More information about the Python-list mailing list