Multiprocess: Pool

Pool(3) creates 3 child-processes and let's them compute the values. map returns the results in the same order as the input came in.

import multiprocessing
import os
import sys

def f(x):
    print(f"Input {x} in process {os.getpid()}")
    return x*x

def main():
    if len(sys.argv) != 3:
        exit(f"Usage: {sys.argv[0]} NUMBERS POOL_SIZE")
    numbers = int(sys.argv[1])
    size    = int(sys.argv[2])

    with multiprocessing.Pool(size) as p:
        results =, range(numbers))

if __name__ == '__main__':

python  11 3
python  100 5