限制迭代器生成的值的数目,直到确认为止。
bounded-iterator的Python项目详细描述
简介
多处理.pool.threadpool和多处理.pool拥有美妙的 imap和imap\u无序的方法,允许使用iterable 并将逐步产生结果,因为他们成为可用的。
这是很好的,因为它允许处理大于可用的输入或结果 记忆。
它有一个重要的缺陷,那就是它不限制结果的数量 没有被消耗掉。这会导致它不受限制地使用内存。
这个包在iterable上实现了一个包装器,它将限制 产生的结果,直到他们被承认。它允许 要同时处理的消息,但不能超过此值。
用法
it=itertools.count()# The input iterable.it=BoundedIterator(10,it)# Allows concurrent processing of up to 10 values.results=Pool().imap(str,it)# Will begin consuming `it` and producing results.time.sleep(5)# No matter what else we do, no more than 10 results will be available.forresinresults:# Consume normally.print(res)it.processed()# Acknowledge a value was processed so that a new one can be generated.