我正在尝试使用多线程和/或多处理来加快我的脚本的速度。本质上,我有一个从CSV读取的10,000个子网的列表,我希望将其转换为IPv4对象,然后存储在数组中。
我的基本代码如下,并在大约300毫秒内执行:
aclsConverted = []
def convertToIP(ip):
aclsConverted.append(ipaddress.ip_network(ip))
for y in acls:
convertToIP(y['srcsubnet'])
如果我尝试使用parallel.futures线程,它可以工作,但速度慢3-4倍,如下所示:
aclsConverted = []
def convertToIP(ip):
aclsConverted.append(ipaddress.ip_network(ip))
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
for y in acls:
executor.submit(convertToIP,y['srcsubnet'])
然后,如果我尝试使用current.futures,将其以慢速处理10-15倍,并且数组为空。代码如下
aclsConverted = []
def convertToIP(ip):
aclsConverted.append(ipaddress.ip_network(ip))
with concurrent.futures.ProcesspoolExecutor(max_workers=20) as executor:
for y in acls:
executor.submit(convertToIP,y['srcsubnet'])
我在其上运行的服务器具有28个物理核心。
任何有关我可能做错事情的建议将不胜感激!