码迷,mamicode.com
首页 > 系统相关 > 详细

进程池

时间:2018-11-09 20:53:29      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:[]   import   int   tip   for   res   返回   pool   pytho   

import multiprocessing as mp
from time import sleep 
import os 

def worker(msg):
    sleep(2)
    print(msg)
    return "worker return" + msg

#创建进程池对象 ,进程池中包含4个进程
pool = mp.Pool(processes = 4)

result = []
for i in range(10):
    msg = "hello %d"%i 
    #向进程池加入要执行的事件
    r = pool.apply_async(worker,(msg,))
    # pool.apply(worker,(msg,))
    result.append(r)

#获取每个事件函数的返回值
for res in result:
    print(res.get())

#关闭进程池事件加入通道
#即不能再向进程池中加入事件
pool.close()
#阻塞等待进程池处理事件结束后回收进程池
pool.join()

#进程池只有4个进程,所以每次执行结果只有四个结果

进程池

标签:[]   import   int   tip   for   res   返回   pool   pytho   

原文地址:https://www.cnblogs.com/-hjj/p/9936738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!