关于python:Python-ThreadPoolExecutor-限制workqueue-大小

3次阅读

共计 501 个字符,预计需要花费 2 分钟才能阅读完成。

应用 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:

  • https://stackoverflow.com/que…
正文完
 0