验证中...
通过使用进程完成多任务。
Raw Copy
import multiprocessing
import os,time,random
def worker(msg):
t_start = time.time()
print("%s开始执行,进程号为%d" % (msg,os.getpid()))
# random.random()随机生成0~1之间的浮点数
time.sleep(random.random()*2)
t_stop = time.time()
print(msg,"执行结束,耗时%0.2f" % (t_stop-t_start))
def main():
po = multiprocessing.Pool(3)
# Pool().apply_async(要调用的目标,(传递给目标的参数元祖))
# 每次循环将会用空闲出来的子进程去调用目标
for i in range(0,10):
po.apply_async(worker,(i,))
print("-- Start --")
po.close()
po.join()
print("-- End --")
if __name__ == "__main__":
main()

Comment list( 0 )

Sign in for post a comment

Help Search