Threads not exiting and program won't exit
<p>Using the script below, I cannot seem to exit the threads. The script runs smoothly without issues but never exits when done. I can still see the thread alive, I have to use <code>htop</code> to kill them or completely exit the command line.</p>

<p>How can I get this script to exit and the threads to die?</p>

<pre><code>def async_dns():
    s = adns.init()
    while True:
        dname = q.get()
        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")
        elif q.empty() == True:
            q.task_done()

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

for name in names:
    q.put_nowait(name)
</code></pre>

<p>Your threads are stuck in <code>dname = q.get()</code></p>

<p>If you reaching empty queue, <code>q.get()</code> will wait forever for value to arrive.</p>

<p>You can replace <code>get</code> with <code>get_nowait()</code> but get ready to catch <code>Queue.Empty</code> execption</p>

python multithreading