Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7442779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:13:41+00:00 2026-05-29T11:13:41+00:00

I programmed a script that should resolve multiple hostnames into ip addresses using Multithreading.

  • 0

I programmed a script that should resolve multiple hostnames into ip addresses using Multithreading.

However, it fails and freezes at some random point. How can this be solved?

num_threads = 100
conn = pymysql.connect(host='xx.xx.xx.xx', unix_socket='/tmp/mysql.sock', user='user', passwd='pw', db='database')
cur = conn.cursor()
def mexec(befehl):
    cur = conn.cursor()
    cur.execute(befehl)

websites = ['facebook.com','facebook.org' ... ... ... ...] \#10.000 websites in array
queue = Queue()
def getips(i, q):
    while True:
        #--resolve IP--
        try:
            result = socket.gethostbyname_ex(site)
            print(result)
            mexec("UPDATE sites2block SET ip='"+result+"', updated='yes' ") #puts site in mysqldb
        except (socket.gaierror):
            print("no ip")
            mexec("UPDATE sites2block SET ip='no ip', updated='yes',")
        q.task_done()
#Spawn thread pool
for i in range(num_threads):
    worker = Thread(target=getips, args=(i, queue))
    worker.setDaemon(True)
    worker.start()
#Place work in queue
for site in websites:
    queue.put(site)
#Wait until worker threads are done to exit
queue.join()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:13:42+00:00Added an answer on May 29, 2026 at 11:13 am

    You could use a sentinel value to signal threads that there is no work and join the threads instead of queue.task_done() and queue.join():

    #!/usr/bin/env python
    import socket
    from Queue import Queue
    from threading import Thread
    
    def getips(queue):
        for site in iter(queue.get, None):
            try: # resolve hostname
                result = socket.gethostbyname_ex(site)
            except IOError, e:
                print("error %s reason: %s" % (site, e))
            else:
                print("done %s %s" % (site, result))
    
    def main():
        websites = "youtube google non-existent.example facebook yahoo live".split()
        websites = [name+'.com' for name in websites]
    
        # Spawn thread pool
        queue = Queue()
        threads = [Thread(target=getips, args=(queue,)) for _ in range(20)]
        for t in threads:
            t.daemon = True
            t.start()
    
        # Place work in queue
        for site in websites: queue.put(site)
        # Put sentinel to signal the end
        for _ in threads: queue.put(None)
        # Wait for completion
        for t in threads: t.join()
    
    main()
    

    gethostbyname_ex() function is obsolete. To support both IPv4/v6 addresses you could use socket.getaddrinfo() instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a custom script programmed and it is using the authors own module
I programmed a bluetooth Television-Remote control for cellphones in J2ME using javax.microedition.lcdui.* (Gauge, List,
I have a webservice programmed in coldfusion which I'm attempting to consume using c#.net.
I have programmed an emulator, but I have some doubts about how to organizate
I am writing a greasemonkey script that is parsing a page with the following
This is my pagination script which extracts info for my TV guide project that
This is for debugging purpose. I've got a for loop that generates some output
I have seen majority of web programmers (writing some script with sql to make
I've written a script that saves its output to a CSV file for later
I want to write a python script to convert PNG's into 2-page pdfs (i.e.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.