Best strategy for overcoming excessive gethostbyname timeout.

r0g aioe.org at technicalbloke.com
Sat Nov 28 02:46:42 EST 2009


r0g wrote:
> Gabriel Genellina wrote:
>> En Fri, 27 Nov 2009 22:35:36 -0300, r0g <aioe.org at technicalbloke.com>
>> escribió:
>>
>>> gethostbyname ignores setdefaulttimeout.
>>>
>>> How big a job is it to use non-blocking sockets to write a DNS lookup
>>> function with a customisable timeout? A few lines? A few hundred? I'd
<snip>

As usual, everything is working beautifully until I try to make it work
with windows!

Turns out signals.SIGALRM is Unix only and I want to run on both
platforms so I have done as the docs suggested and tried to convert the
code to use threading.Timer to trigger an exception if the DNS lookup is
taking too long.

It's behaviour seems quite odd.

The exception is raised on schedule after a couple of seconds and
appears in the terminal (despite me not printing it to the terminal!).
There's then a pause of nearly 30 seconds before the try/except catches
it and proceeds as normal. I can see no reason for a delay between the
error being raised and caught, unless whatever it's trying to interrupt
is blocking, in which case it shouldn't have been interrupted by the
signal either no? So anyway my questions are...

Why the error message and can it be suppressed?
Why the 30 second delay, what's it doing during that time?

Code...

        timeout_timer = threading.Timer(DNS_TIMEOUT, dns_timeout)
        timeout_timer.start()
        try:
            dest_addr = DNSResolve( dest_addr )
        except Exception:
            print "Caught an exception, returning False"
            try:
                timeout_timer.cancel()
            except:
                None
            return False

Other code as before.

On the face of it it looks like it might be dwelling in the .req()
method of the pydns library but it can get confusing quickly with
threading so I'm far from sure.

On top of that I'm not sure if my threads are dying and being cleared up
properly. Each Exception I raise shows the Thread number getting higher
e.g. "Exception in thread Thread-8:". Do I need to be doing anything to
clear these up or does Python just use a monotonic counter for thread names?

Is there a less messy way to use signals on both Unix and Windows? The
docs seem to suggest they've kludged the signals module into a workable
form most platforms but on XP I get 'module' object has no attribute
'SIGALRM'

:(

Roger.



More information about the Python-list mailing list