This gist provides a function that acts like concurrent.futures.ThreadPoolExecutor.map(fn, it),
but it extracts it lazily rather than eagerly.
Works with both ThreadPoolExecutor and ProcessPoolExecutor.
from lazy_executor_map import lazy_executor_map
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor() as ex:
for output in lazy_executor_map(work, out_of_core_inputs, ex):
...This is useful for out-of-core iterator pipelines where the entire data source behind out_of_core_inputs doesn''t fit in memory.
One way to implement efficient io multitasking is via ThreadPoolExecutor, which is an implementation of concurrent.futures.Executor. As concurrent.futures.Executor.map(fn, it) collects it eagerly,
calling pool.map(fn, dataset) will collect the entire dataset into memory even if dataset is a streaming iterator.
This makes it unsuitable for out-of-core operation. However, it is possible to get this to behave lazily instead. This gist implements that functionality.