我正在使用python線程來解析網站IP地址。這是我解決問題的工作流程。這是一個守護線程。Python線程似乎凍結了機器
def get_ip_worker():
"""This is the worker (thread) process for parsing ips, this process takes domain from the q processes it
and then saves it to another q"""
socket.setdefaulttimeout(3)
while True:
domain = domains_q.get()
try:
addr_info = socket.getaddrinfo(domain, 80, 0, 0, socket.SOL_TCP)
for family, socktype, proto, name, ip in addr_info:
if family == 2: #okay it's ipv4
ip, port = ip
processed_q.put((ip, domain))
elif family == 10: #okay it's ipv6
ip, port, no_1, no_2 = ip
processed_q.put((ip, domain))
except:
pass
#print 'Socket Error'
domains_q.task_done()
編輯:域= domains_q.get()這條線塊,直到產品在隊列可用。
問題是當我在300個線程運行此,平均負載似乎不錯,但簡單的LS -la需要5秒,一切都是緩慢的。我哪裏做錯了?我應該使用異步還是多處理?
你確定空隊列異常被打破循環? – andsoa 2013-03-11 15:39:42
domains_q.get()此行會阻止,直到某個項目可用,我已將其添加到帖子中。 – nacholibre 2013-03-11 15:45:35