Python套接字.gethostbyname_ex()多线程失败

2024-10-03 02:48:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个脚本,该脚本应该使用多线程将多个主机名解析为ip地址。在

但是,它会在某个随机点发生故障并冻结。如何解决这个问题?在

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()

Tags: inip脚本queuesitesocketresultconn
3条回答

我的第一个想法是,你会因为DNS过载而出错——也许你的解析器不允许你每次执行超过一定数量的查询。在


此外,我发现了一些问题:

  1. 您忘了在while循环中正确地分配site这可能最好用在队列上迭代的for循环来代替。在您的版本中,您使用模块级命名空间中的site变量,这可能导致查询加倍,其他查询被跳过。在

    在这种情况下,您可以控制队列是否仍有条目或等待一些条目。如果两者都不是,你可以退出你的线程。

  2. 出于安全考虑,你最好这样做

    def mexec(befehl, args=None):
        cur = conn.cursor()
        cur.execute(befehl, args)
    

    为了以后做

    mexec("UPDATE sites2block SET ip=%s, updated='yes'", result) #puts site in mysqldb
    

为了与未来的协议保持兼容,您应该使用socket.getaddrinfo(),而不是{}。在那里你可以得到你想要的所有IP(一开始,你可以限制到IPv4,但是切换到IPv6就更容易了),并且可以将它们全部放入数据库。在


对于您的队列,代码示例可以是

def queue_iterator(q):
    """Iterate over the contents of a queue. Waits for new elements as long as the queue is still filling."""
    while True:
        try:
            item = q.get(block=q.is_filling, timeout=.1)
            yield item
            q.task_done() # indicate that task is done.
        except Empty:
            # If q is still filling, continue.
            # If q is empty and not filling any longer, return.
            if not q.is_filling: return

def getips(i, q):
    for site in queue_iterator(q):
        # resolve IP 
        try:
            result = socket.gethostbyname_ex(site)
            print(result)
            mexec("UPDATE sites2block SET ip=%s, updated='yes'", result) #puts site in mysqldb
        except (socket.gaierror):
            print("no ip")
            mexec("UPDATE sites2block SET ip='no ip', updated='yes',")
# Indicate it is filling.
q.is_filling = True
#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)
queue.is_filling = False # we are done filling, if q becomes empty, we are done.
#Wait until worker threads are done to exit
queue.join()

应该会成功的。在


另一个问题是并行插入MySQL。一次只能执行一个MySQL查询。因此,您可以通过threading.Lock()RLock()来保护访问,也可以将答案放入另一个由另一个线程处理的队列中,该队列甚至可以捆绑它们。在

您可能会发现使用^{}比直接使用threadingmultiprocessingQueue更简单:

#!/usr/bin/env python3
import socket
# pip install futures on Python 2.x
from concurrent.futures import ThreadPoolExecutor as Executor

hosts = "youtube.com google.com facebook.com yahoo.com live.com".split()*100
with Executor(max_workers=20) as pool:
     for results in pool.map(socket.gethostbyname_ex, hosts, timeout=60):
         print(results)

注意:您可以轻松地从使用线程切换到进程:

^{pr2}$

如果gethostbyname_ex()在您的操作系统上不是线程安全的,例如it might be the case on OSX,则需要它。在

如果要处理gethostbyname_ex()中可能出现的异常:

import concurrent.futures

with Executor(max_workers=20) as pool:
    future2host = dict((pool.submit(socket.gethostbyname_ex, h), h)
                       for h in hosts)
    for f in concurrent.futures.as_completed(future2host, timeout=60):
        e = f.exception()
        print(f.result() if e is None else "{0}: {1}".format(future2host[f], e))

它类似于the example from the docs。在

您可以使用sentinel值向线程发出没有工作的信号,并连接线程,而不是queue.task_done()和{}:

#!/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()函数已过时。要同时支持IPv4/v6地址,可以使用^{}。在

相关问题 更多 >