Python ThreadPoolExecutor 限制_work_queue 大小

NoCode
·

使用python的futures.ThreadPoolExecutor是,如果调用submit提交任务 ThreadPoolExecutor的会向执行

self._work_queue.put(w)

其中

self._work_queue = queue.SimpleQueue()

SimpleQueue 是不限制队列大小的,如果提交的任务太多,处理不及时,则导致占用太多内存

可以替换到_work_queue的实现,使用queue.Queue(maxsize=maxsize)

class ThreadPoolExecutorWithQueueSizeLimit(futures.ThreadPoolExecutor):
    def __init__(self, maxsize=50, *args, **kwargs):
        super(ThreadPoolExecutorWithQueueSizeLimit, self).__init__(*args, **kwargs)
        self._work_queue = queue.Queue(maxsize=maxsize)

links:

社区准则 博客 联系 社区 状态
主题