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