O_NONBLOCK vs. O_NDELAY for non-blocking sockets

Hrvoje Niksic hniksic at srce.hr
Mon May 24 21:46:51 EDT 1999


Apparently the setblocking() method of socket objects is setting the
O_NDELAY flag to set the socket to non-blocking mode.  However, _POSIX
Programmer's Guide_ by Donald Lewine says:

O_NONBLOCK:     Do not wait for the device or file to be ready or
                available.  After the file is open, the read() and
                write() calls always return immediately.  If the
                process would be delayed in the read or write
                operation, -1 is returned and errno is set to EAGAIN
                instead of blocking the caller.

                System V provides a flag called O_NDELAY that is
                similar to O_NONBLOCK.  The O_NDELAY flag causes
                read() or write() to return zero instead of blocking.
                Since read() also returns zero on end-of-file, it is
                difficult to distinguish the two cases.  BSD also has
                an O_NDELAY flag that causes the error EWOULDBLOCK to
                be returned if the process would block.  POSIX
                resolved this incompatibility by inventing the
                O_NONBLOCK flag.  Port with care!

Based on this, I believe Python should use O_NONBLOCK where available, 
O_NDELAY otherwise.  On Linux, both are the same thing, but on Solaris 
they are not, and O_NONBLOCK should be preferred.




More information about the Python-list mailing list