Python 异步 FQDN 解析

Python Asynchronous FQDN Resolution

我有一个 10k ips 的列表,我需要获取他们的 FQDN。同步执行此操作需要很长时间,因此我尝试对其进行异步编码,但我没有发现执行时间有任何差异。

同步方式:

def test_synch():
    start_time = time.time()
    for ip in ip_list:
        fqdn = socket.getfqdn(ip)
        print(fqdn)
    print("Time for synchronous requests: ", time.time()-start_time)

执行时间:100 个 ip 地址 284 秒

异步方法:

async def get_fqdn_async(ip):
    return socket.getfqdn(ip)


async def get_fqdn(ip):
    print("executed task for ip", ip)
    fqdn = await get_fqdn_async(ip)
    print("got fqdn ", fqdn, " for ip ", ip)
    return fqdn


async def main():
    tasks = []
    for ip in ip_list:
        task = asyncio.create_task(
            get_fqdn(ip))
        tasks.append(task)

    fqdns = await asyncio.gather(*tasks)
    print(fqdns)

def test_asynch():
    start_time = time.time()
    asyncio.run(main())
    print("Time for asynchornous requests: ", time.time()-start_time)

执行时间:100 ips 283 秒

显然我做错了什么,但我不知道是什么。

,在我看来多线程在这里是最理想的。考虑一下:

from concurrent.futures import ThreadPoolExecutor
import socket
import json

list_of_ips = ['www.google.com', 'www.bbc.co.uk', 'www.tripdavisor.com', 'www.whosebug.com', 'www.facebook.com']

def getfqdn(ip):
    return ip, socket.getfqdn(ip)

results = dict()
with ThreadPoolExecutor() as executor:
    for future in [executor.submit(getfqdn, ip) for ip in set(list_of_ips)]:
        ip, fqdn = future.result()
        results[ip] = fqdn

with open('report.json', 'w') as j:
    json.dump(results, j, indent=4)