我編程應該可以解決多個主機名到使用多線程的IP地址的腳本。的Python socket.gethostbyname_ex()多線程失敗
但是,它未能在一些隨機點凍結。這怎麼解決?
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()
你會得到什麼錯誤? – 2012-02-08 13:59:17
sry,忘了接受!我沒有得到具體的錯誤,腳本運行,並在某些時候只是凍結而不顯示任何具體的錯誤。然後我必須殺死殼。 – user670186 2012-02-08 14:10:31
這個例子是不完整的 - 什麼是mexec? – 2012-02-08 14:15:47