gethostbyname blocking

marc wyburn marc.wyburn at googlemail.com
Wed Apr 22 11:08:51 EDT 2009


Hi, I am writing an asynchronous ping app to check if 1000s of hosts
are alive very quickly.  Everything works extremely quickly unless the
host name doesn't have a DNS record.

when calling socket.gethostbyname if there is no record for the host
the result seems to block all other threads.  As an example I have 6
threads running and if I pass the class below a Queue with about 30
valid addresses with one invalid address in the middle the thread that
the exception occurs in seems to block the others.

I'm pretty new to threading so am not sure if my threading code is
wrong or whether I need to look at how gethostbyname?  When there list
is made entirely of valid host names I get the responses back very
very quickly.

class HostThread(threading.Thread):
    def __init__(self, Host_Queue):
        threading.Thread.__init__(self)
        self.Host_Queue = Host_Queue
    def run(self):
        while True:
            # Create a new IP packet and set its source and
destination addresses.
            host = self.Host_Queue.get()
            try:
                dest_addr, alias, ipaddrlist  =
socket.gethostbyname_ex(host)
            except socket.gaierror, (errorno, details):
                print '%s: %s' %(host,details)
            else:
                print '%s: %s' %(host, ipaddrlist)
            self.Host_Queue.task_done()

Thanks, Marc.



More information about the Python-list mailing list