multiprocessing

Table of Contents

Overview

from multiprocessing import Pool
import time

def f(id_):
    for _ in range(2):
        print id_
        time.sleep(1)


# pool must be created AFTER defining functions to apply
pool = Pool(processes=3)

for i in range(4):
    pool.apply_async(f, (i,))

pool.close()  # Prevents any more tasks from being submitted to the pool
pool.join()