socket setdefaulttimeout

Bryan Olson fakeaddress at nowhere.org
Sat Aug 13 01:37:22 EDT 2005


Sheila King wrote:
 > I'm doing DNS lookups [...] it is important to make sure
 > that the socket doesn't just hang there waiting for a response.
 >
 > After a recent system upgrade to Python 2.4.1 (from 2.2.2) I thought I
 > could take advantage of the setdefaulttimeout in the socket module, to
 > limit the amount of time the sockets take for a lookup.
 >
 > As a test, I set the default timout ridiculously low. But it doesn't
 > seem to be having any effect.

The timeout applies to network communication on that socket, but
not to calls such as socket.gethostbyname. The gethostbyname
function actually goes to the operating system, which can look
up the name in a cache, or a hosts file, or query DNS servers
on sockets of its own.

Modern OS's generally have reasonably TCP/IP implementations,
and the OS will handle applying a reasonable timeout. Still
gethostbyname and its brethren can be a pain for single-
threaded event-driven programs, because they can block for
significant time.

Under some older threading systems, any system call would block
every thread in the process, and gethostbyname was notorious for
holding things up. Some systems offer an asynchronous
gethostbyname, but that doesn't help users of Python's library.
Some programmers would keep around a few extra processes to
handle their hosts lookups. Fortunately, threading systems are
now much better, and should only block the thread waiting for
gethostbyname.


-- 
--Bryan



More information about the Python-list mailing list