我有一个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)执行时间: 283秒,100 ips
显然我做错了什么,但我想不出是什么。
发布于 2021-12-17 14:32:30
,在我看来,多线程在这里是最理想的。考虑到这一点:
from concurrent.futures import ThreadPoolExecutor
import socket
import json
list_of_ips = ['www.google.com', 'www.bbc.co.uk', 'www.tripdavisor.com', 'www.stackoverflow.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)https://stackoverflow.com/questions/70394525
复制相似问题