Created
January 21, 2015 12:35
-
-
Save majkrzak/06bbd83eccd4083c68d0 to your computer and use it in GitHub Desktop.
This file contains 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
#setattr(ThreadPoolExecutor,'imap',__import__('imap').imap) | |
from collections import deque | |
def imap(self, fn, *iterables, timeout=None, limit=None): | |
if timeout is not None: | |
end_time = timeout + time.time() | |
if limit is None: | |
limit = self._max_workers | |
argv = zip(*iterables) | |
fs = deque([self.submit(fn, *next(argv)) for i in range(limit)]) | |
def result_iterator(): | |
try: | |
while True: | |
future = fs.popleft() | |
if timeout is None: | |
yield future.result() | |
else: | |
yield future.result(end_time - time.time()) | |
fs.append(self.submit(fn, *next(argv))) | |
finally: | |
for future in fs: | |
future.cancel() | |
return result_iterator() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment