Created
July 19, 2014 06:19
-
-
Save ikuyamada/7bdbebc5f4ea70a7e8ed to your computer and use it in GitHub Desktop.
multiprocessing's imap() wrapper that lazily creates chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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