Queue runs successfully! Exits instead of continue, after Break
<p>I am using this script to resolve thousands of domains. It runs successfully, and ends when the queue is empty. I am trying to it to break out of the loop and continue the script by printing.</p>

<p>How do I get this code to break out of the loop, ans print, when the queue is empty?</p>

<pre><code>q = queue.Queue()
for name in names:
    q.put(name)

def async_dns():
    s = adns.init()

    while True:
        try:
            dname = q.get(False)
            q.task_done()
        except queue.Empty:
            return 
        response = s.synchronous(dname,adns.rr.NS)[0]
        if response == 0:
            dot_net.append("Y")
            print(dname + ", is Y")
        elif response == 300 or response == 30 or response == 60:
            dot_net.append("N")
            print(dname + ", is N")


threads = []
for i in range(20):
    t = threading.Thread(target=async_dns)
    threads.append(t)
    t.start()


  print("Done !!")
</code></pre>

<p>You could simply move the code that does the dns lookup and prints the result into the body of the <code>try/except</code> block:</p>

<pre><code>def async_dns():
    s = adns.init()

    while True:
        try:
            dname = q.get(False)

            response = s.synchronous(dname,adns.rr.NS)[0]
            if response == 0:
                dot_net.append("Y")
                print(dname + ", is Y")
            elif response == 300 or response == 30 or response == 60:
                dot_net.append("N")
                print(dname + ", is N")

            q.task_done()
        except queue.Empty:
            return 
</code></pre>

<p>Now when the queue is empty a <code>queue.Empty</code> will be raised and the exception handler will simply exit the thread function, otherwise it will print out the dns values.</p>

python queue