Nonblocking sockets in Python
Blocking I/O is the scourge of reliable Internet programs. Python’s
simple network libraries like urllib are surprisingly ænemic with
respect to timeout options. Happily, Python 2.3 includes two new
functions in the socket
module: getdefaulttimeout and setdefaulttimeout.
socket.setdefaulttimeout(5)
try:
urllib.urlopen(url).read()
except IOError, e:
# handle timeout
There are more sophisticated ways to do nonblocking I/O in Python, but
for simple stuff this works.
try:
urllib.urlopen(url).read()
except IOError, e:
# handle timeout