Skip to content

Instantly share code, notes, and snippets.

@majkrzak
Created January 21, 2015 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majkrzak/06bbd83eccd4083c68d0 to your computer and use it in GitHub Desktop.
Save majkrzak/06bbd83eccd4083c68d0 to your computer and use it in GitHub Desktop.
#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