Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  1. Should sockets be set to non-blocking when used with select.select in Python?
  2. What difference does it make if they are or aren't?

Occasionally I find that calling send on a socket that returns sendable will block. Furthermore I find that blocking sockets will generally send the whole buffer given (128 KiB). In non-blocking mode, sending will accept far fewer bytes (20-40 KiB compared with the example given earlier) and return quicker. I'm using Python 3.1 on Lucid.

like image 210
Matt Joiner Avatar asked Mar 08 '11 20:03

Matt Joiner


1 Answers

The answer might be OS dependent unfortunately. I'm replying only regarding Linux.

I'm not aware of differences regarding blocking/non-blocking sockets in select, but on linux, the select system call man page has this in it 'BUGS' section:

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.

I doubt a python abstraction above that could "hide" this issue without side-effects.

As for the blocking write sending more data, that's expected. send will block until there is enough buffer space to pass your whole request down if the socket is blocking. If the socket is non-blocking, it only sends as much as can currently fit in the socket's send buffer.

like image 152
Mat Avatar answered Sep 24 '22 06:09

Mat