Python 2.2.1 and select()

Derek Martin code at pizzashack.org
Wed Mar 26 19:59:46 EDT 2008


On Wed, Mar 26, 2008 at 09:49:51AM -0700, Noah Spurrier wrote:
> On 2008-03-24 22:03-0400, Derek Martin wrote:
> >That's an interesting thought, but I guess I'd need you to elaborate
> >on how the buffering mode would affect the operation of select().  I
> >really don't see how your explanation can cover this, given the
> >following:
> 
> I might be completely off the mark here. I have not tested your code or even
> closely examined it. I don't mean to waste your time. I'm only giving a
> reflex response because your problem seems to exactly match a very common
> situation where someone tries to use select with a pipe to a subprocess
> created with popen and that subprocess uses C stdio. 

Yeah, you're right, more or less.  I talked to someone much smarter
than I here in the office, who pointed out that the behavior of
Python's read() without a specified size is to attempt to read until
EOF.  This will definitely cause the read to block (if there's I/O
waiting from STDERR), if you're allowing I/O to block... :(

The solution is easy though... 

def set_nonblock(fd):
 	flags = fcntl.fcntl(fd, fcntl.F_GETFL)
	fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)

Then in the function, after calling popen:
 	set_nonblock(io.fromchild.fileno())
	set_nonblock(io.childerr.fileno())

Yay for smart people.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080326/cd667822/attachment-0001.sig>


More information about the Python-list mailing list