multiprocessing Pool

import multiprocessing as mp
import threading as td
import time


def job(x):
	return x * x


def multicore():
	pool = mp.Pool()
	res = pool.map(job, range(10))
	print("pool result:", res)
	res = pool.apply_async(job, (2,))
	# 用get获得结果
	print("apply_async:", res.get())
	# 迭代器,i=0时apply一次,i=1时apply一次等等
	multi_res = [pool.apply_async(job, (i,)) for i in range(10)]
	# 从迭代器中取出
	print("apply_async iteration:", [res.get() for res in multi_res])


if __name__ == '__main__':
	multicore()

猜你喜欢

转载自blog.csdn.net/akon_wang_hkbu/article/details/79869109