example_multiprocessing.py 495 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import time
from multiprocessing import Pool


def f(duration=5):
    """ this function will keep working for ``duration`` seconds """
    a = 0
    t0 = time.time()
    while time.time() - t0 < duration:
        a += 1
    return


if __name__ == '__main__':
    t_start = time.time()
    # using ``with ... as ...`` clause helps avoid ``Pool.close`` after the context
    with Pool(5) as p:
        p.map(f, [5, 5, 5, 5, 5])
    print("Total time cost: {} sec!".format(time.time() - t_start))