A typical approach is to use select () until the data is available or until a timeout occurs. Call recv() only when data is actually available. To be safe, we also set the socket to non-blocking mode to ensure that recv() will never block indefinitely. select() can also be used to wait for more than one socket at a time.
import select mysocket.setblocking(0) ready = select.select([mysocket], [], [], timeout_in_seconds) if ready[0]: data = mysocket.recv(4096)
If you have many open file descriptors, poll () is a more efficient alternative to select() .
Another option is to set a timeout for all operations on the socket using socket.settimeout() , but I see that you explicitly rejected this solution in a different answer.
Daniel Stutzbach Apr 27 '10 at 13:49 on 2010-04-27 13:49
source share