def hhh(i):
return i ** 3
pool = Pool(processes=3)
h1 = pool.map(hhh, iterable=[1, 2, 3, 4])
print(h1)
[1, 8, 27, 64]
h2 = pool.map_async(hhh, iterable=[1, 2, 3, 4])
for i in h2.get():
print(i)
1
8
27
64
import time
from multiprocessing import Pool
def run(msg):
print('msg:%s' %msg)
# 程序随眠3秒,
time.sleep(3)
print("end")
def callback():
print("i am callback")
print("开始执行主程序")
start_time=time.time()
# 使用进程池创建子进程
size=3
pool=Pool(size)
print("开始执行子进程")
for i in range(size):
pool.apply(run, kwds={"msg":1})
# pool.apply_async(run, kwds={"msg":1},callback=callback())
print("主进程结束耗时%s"%(time.time() - start_time))
开始执行主程序
msg:1
开始执行子进程
end
msg:1
end
msg:1
end
主进程结束耗时9.1600182056427