文章内容
2021/8/20 18:57:08,作 者: 黄兵
futures.ThreadPoolExecutor 内存耗尽相关问题
在这篇文章中讲解了如何使用多线程长时间运行程序的相关问题:python 多线程长时间运行程序
运行一段时间之后,内存会耗尽,具体截图如下:
重启服务之后,内存又恢复,具体代码如下:
workers = min(self._num_worker, len(ip_list) if len(ip_list) > 0 else 1)
with futures.ThreadPoolExecutor(workers) as executor:
to_do = []
for ip in ip_list:
future = executor.submit(self.traceroute, ip)
to_do.append(future)
for future in futures.as_completed(to_do):
future.result()
解决方案:
def traceroute_main(self, ip_list):
workers = min(self._num_worker, len(ip_list) if len(ip_list) > 0 else 1)
with futures.ThreadPoolExecutor(workers) as executor:
to_do = []
for ip in ip_list:
future = executor.submit(self.traceroute, ip)
to_do.append(future)
for future in futures.as_completed(to_do):
future._result = None
参考资料:
1、concurrent.futures --- 启动并行任务
2、Using Concurrent Futures without running out of RAM
黄兵个人博客原创。
评论列表