python Threadpoolexcutor导致内存爆满

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

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

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()

猜你喜欢

转载自www.cnblogs.com/wtil/p/12752303.html