[Python-Dev] Draft PEP to make file objects support non-blocking mode. (original) (raw)

Peter Astrand astrand at lysator.liu.se
Mon Mar 21 11:42:47 CET 2005


On Mon, 21 Mar 2005, Donovan Baarda wrote:

> > The only ways to ensure that a select process does not block like this, > > without using non-blocking mode, are;

> 3) Use os.read / os.write. [...]

but os.read / os.write will block too.

No.

Try it... replace the file read/writes in selector.py. They will only do partial reads if the file is put into non-blocking mode.

I've just tried it; I replaced:

data = o.read(BUFF_SIZE)

with:

data = os.read(o.fileno(), BUFF_SIZE)

Works for me without any hangs. Another example is the subprocess module, which does not use non-blocking mode in any way. (If you are using pipes, however, you shouldn't write more than PIPE_BUF bytes in each write.)

> > I think the fread/fwrite and read/write behaviour is posix standard and > > possibly C standard stuff... so it should be the same on other > > platforms. > > Sorry if I've misunderstood your point, but fread()/fwrite() does not > return EAGAIN.

no, fread()/fwrite() will return 0 if nothing was read/written, and ferror() will return EAGAIN to indicated that it was a "would block" condition.... at least I think it does... the man page simply says ferror() returns a non-zero value.

fread() should loop internally on EAGAIN, in blocking mode.

/Peter Åstrand <astrand at lysator.liu.se>



More information about the Python-Dev mailing list