并行化
from functools import partial
import multiprocessing
import time
def f(a, b, c):
print("{} {} {}".format(a, b, c))
def main():
iterable = [1, 2, 3, 4, 5]
cores = multiprocessing.cpu_count()
pool = multiprocessing.Pool(precesses=cores)
a = "hi"
b = "there"
func = partial(f, a, b) # a, b作为f的前两个参数,固定下来,构造成只包含一个参数的新函数 func
pool.map(func, iterable) # 并行化实现的 map,调用多个cores
pool.close()
pool.join()
if __name__ == "__main__":
start = time.time() # 计时函数,如果使用 time.clock() 计时就会少计,不知道为啥
main()
elapsed = time.time() - start
print("Time used:", elapsed)