Skip to content

Instantly share code, notes, and snippets.

@lucaswiman
Created April 17, 2019 23: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 lucaswiman/c6477d3c9a58ee1998f2a84944407219 to your computer and use it in GitHub Desktop.
Save lucaswiman/c6477d3c9a58ee1998f2a84944407219 to your computer and use it in GitHub Desktop.
from typing import Callable, Iterable, T
from multiprocessing.pool import ThreadPool
def threaded_map(f: Callable[..., T], it: Iterable, num_threads: int) -> Iterable[T]:
pool = ThreadPool(num_threads)
try:
results = pool.map(f, it)
finally:
pool.close()
pool.join()
yield from results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment