Skip to content

Instantly share code, notes, and snippets.

@ikuyamada
Created July 19, 2014 06:19
Show Gist options
  • Save ikuyamada/7bdbebc5f4ea70a7e8ed to your computer and use it in GitHub Desktop.
Save ikuyamada/7bdbebc5f4ea70a7e8ed to your computer and use it in GitHub Desktop.
multiprocessing's imap() wrapper that lazily creates chunks
from multiprocessing import Pool
from itertools import islice
def imap_lazy(func, iterable, pool_size=10, chunk_size=10000,
maxtasksperchild=1000, unordered=False):
p = Pool(processes=pool_size, maxtasksperchild=maxtasksperchild)
while True:
chunk = list(islice(iterable, chunk_size))
if not chunk:
break
imap_func = p.imap_unordered if unordered else p.imap
for ret in imap_func(func, chunk):
yield ret
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment