原因: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()