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

 

相关文章:

  • 2021-10-07
  • 2022-12-23
  • 2021-09-08
  • 2018-01-30
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
猜你喜欢
  • 2021-11-05
  • 2021-11-20
  • 2021-12-25
  • 2021-11-22
  • 2021-11-25
  • 2018-03-12
  • 2021-08-07
相关资源
相似解决方案