Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active May 27, 2025 09:45
Show Gist options
  • Save vadimkantorov/b25b8bf582e2d69efd57f704515ca96f to your computer and use it in GitHub Desktop.
Save vadimkantorov/b25b8bf582e2d69efd57f704515ca96f to your computer and use it in GitHub Desktop.
Example of using multiprocessing with explicitly batched inputs
import multiprocessing
import itertools
inputs = list(range(111))
batchsize = 10
num_workers = 4
batches = itertools.batched(inputs, batchsize)
def reducer(xs):
return (sum(xs), len(xs))
with multiprocessing.Pool(processes = num_workers) as pool:
print(list(pool.map(reducer, batches)))
def processor(xs):
return [x + 1 for x in xs]
with multiprocessing.Pool(processes = num_workers) as pool:
print(sum(pool.imap_unordered(processor, batches), []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment