python Threadpoolexcutor导致内存爆满


原因:Threadpoolexcutor默认使用的是无界队列,如果消费任务的速度低于生产任务,那么会把生产任务无限添加到无界队列中。导致内存被占满

解决方案:修改无界队列为有界队列

 
 
from concurrent.futures import ThreadPoolExecutor
import queue

class
BoundedThreadPoolExecutor(ThreadPoolExecutor): def __init__(self, max_workers=None, thread_name_prefix=''): super().__init__(max_workers, thread_name_prefix) self._work_queue = queue.Queue(self._max_workers * 2) # 队列大小为最大线程数的两倍 def fun(i): time.sleep(5) print(i) if __name__ == '__main__': t = BoundedThreadPoolExecutor() for i in range(1000000000): t.submit(fun, i) t.shutdown()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM