the use of multiprocessing python library Pool.map quickly create multi-threaded and multi-parameter

 

from multiprocessing import Pool

def cal(item):
    n, mod = item
    res = 1
    for i in range(2, n+1):
        res = res * i % mod
    return res

if __name__ == "__main__":
    args = []
    for i in range(1, 10):
        args.append((i,1000000007))
    with Pool(10) as pool:
        results = pool.map(cal, args)
    print(results)

 operation result:

[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

 

Guess you like

Origin www.cnblogs.com/nervendnig/p/12559124.html