non-blocking sockets

Timothy O'Malley timo at alum.mit.edu
Wed May 1 00:39:17 EDT 2002


hola.

The complexity of the answer could depend on the depth
of your question.  Do you really have *one* socket that
you are trying to read from?  Are you managing a host of
sockets, reading from each as it becomes ready?

The simple answer:
The select() module gives you the ability to check if a
socket has data available.

Another helpful tidbit:
Use the timeoutsocket module!

  http://www.timo-tasi.org/python/timeoutsocket.py

This module gives you the ability to set a specific
timeout value on any TCP socket.


In either case, I'd suggest using blocking sockets over
non-blocking ones to start.  Move to non-blocking sockets
when you comfortably have select() under your belt.

TimO


so very tired <g0riaman at cdf.toronto.edu> wrote:

 > When trying to read from a socket, I don't want it hanging, so I set it to
 > non-blocking by calling
 > 
 > setblocking(0)
 > 
 > but then when I try to read from it and there is no data in the socket, I
 > get an error. I tried this in both windows and linux and in both I get an
 > error and the program stops. It says there's an exception but it doesn't
 > say the name of the exception so I can't even try to catch.
 > Does anyone know how I can check if there's data coming from a socket
 > connection without hanging or having the program barf on me?
 > Thanks.
 > mRiaz
 > 
 >



More information about the Python-list mailing list