Re: [Python-Dev] asyncore fixes in Python 2.6 broke Zope's version of medusa
Greg Ewing wrote:
Antoine Pitrou wrote:
For starters, since py3k is supposed to support non-blocking IO, why not write a portable API to make a raw file or socket IO object non-blocking?
I think we need to be clearer what we mean when we talk about non-blocking in this context. Normally when you're using select/poll you *don't* make the underlying file descriptor non-blocking in the OS sense. The non-blockingness comes from the fact that you're using select/poll to make sure the fd is ready before you access it.
So I don't think it makes sense to talk about having a non-blocking API as a separate thing from a select/poll wrapper. The select/poll wrapper *is* the non-blocking API.
This is not necessarily the case. In fact, modern sources often recommend using poll along with the non-blocking sockets for (among other things) performance reasons. For example, when a non-blocking socket becomes readable, you don't read from it only once and go back to the event loop, you read from it in a loop until you get EAGAIN. This allows for processing of fast-incoming data with fewer system calls. Linux's select(2) man page includes a similar advice with different motivation: Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block. Even if you don't agree that using O_NONBLOCK with select/poll is the best approach to non-blocking, I think there is enough existing practice of doing this to warrant separate consideration of non-blocking sockets (in the OS sense) and select/poll.
Hrvoje Niksic wrote:
Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks.
Blarg. Linux is broken, then. This should not happen.
This could for example happen when data has arrived but upon examination has wrong checksum and is discarded.
That's no excuse -- the kernel should check all its checksums *before* waking up selecting processes!
Even if you don't agree that using O_NONBLOCK with select/poll is the best approach to non-blocking, I think there is enough existing practice of doing this to warrant separate consideration of non-blocking sockets (in the OS sense) and select/poll.
I'm not saying there isn't merit in having support for non-blocking file descriptors, only that it's not in any sense a prerequisite or first step towards a select/poll wrapper. They're orthogonal issues, even if you might sometimes want to use them together. -- Greg
On 10:01 am, greg.ewing@canterbury.ac.nz wrote:
Hrvoje Niksic wrote:
Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks.
Blarg. Linux is broken, then. This should not happen.
You know what else is broken? MacOS, FreeBSD, Solaris, and every version of Windows. I haven't tried running Twisted on AmigaOS but I bet it has some problems too. On occasion Linux has been so badly broken that Twisted has motivated a fix. For example, http://lkml.indiana.edu/hypermail/linux/kernel/0502.3/1160.html But even if we ignore difficulties at the OS level (which should, after all, be worked around rather than catered to in API design) there are other good reasons why the general async API should be fairly distant from both the select/poll wrapper and the questions of blocking vs. non- blocking sockets. For another example, consider the issue of non- blocking SSL sockets. Sometimes, in order to do a "read", you actually need to do a write and then another read. Which means that application code, if it wants to be compatible with SSL, needs to deal with any failure that may come from a write as coming from a read, unless you abstract all this away somehow.
participants (3)
-
glyph@divmod.com
-
Greg Ewing
-
Hrvoje Niksic