Should sockets be non-blocking to work with select in Python?

Sometimes I find that a send call on a socket that returns sendable will block. In addition, I found that blocking sockets usually send the entire specified buffer (128 KiB). In non-blocking mode, sending will take much less bytes (20-40 KiB compared to the above example) and will return faster. I am using Python 3.1 on Lucid .

+4
source share
1 answer

The answer may be OS dependent. I answer only on Linux.

I do not know the differences regarding blocking / non-blocking sockets in select, but in linux on the select system call control page there is this in the "ERRORS" section:

On Linux, select () can tell the socket file descriptor to be "ready to read," though, however, subsequent read blocks. It could, it can for example, when the data has arrived, but after the wrong checksum is discarded. There may be other circumstances in which the file descriptor is falsely reported as ready. Thus, it may be safer to use O_NONBLOCK on sockets that should not block.

I doubt the python abstraction above, which can "hide" this problem without side effects.

As for locking the record sending more data, this was expected. send will block until there is enough buffer space to send the entire request down if the socket is blocked. If the socket is not blocked, it sends only as much as it can insert into the socket send buffer.

+5
source

Source: https://habr.com/ru/post/1342889/


All Articles