why does socket.makefile require non-blocking mode?

Bryan Olson fakeaddress at nowhere.org
Sat Mar 29 07:46:20 CET 2008


Forest wrote:
> The socket.makefile() docs say, "the socket must be in blocking mode."  I
> don't see any explanation of why blocking mode is required, and I'm not sure
> whether that means timeout mode is forbidden as well.  Can someone clarify
> this?

Looking at the code for the existing _fileobject's read method, it
will loose data it has already read if a socket.recv() call raises
an exception. The function keeps buffers in a local variable that
will be lost if an exception exits the scope. That much could be
fixed with a try...finally. Other methods have similar problems.

> I wanted to use file-like objects with socket timeouts, so I ended up writing
> my own replacement for socket._fileobject.  I'd appreciate it if someone could
> either explain to my why my new class was unnecessary, or else encourage me to
> contribute it as a patch to the socket module.

Sure, fix it. A harder problem is that it doesn't play nice with select().


-- 
--Bryan






More information about the Python-list mailing list