python的线程池和进程池很pythonic
进程
<pre>from multiprocessing import Pool
def hello(name):
print('hello ' + name)
if __name__ == '__main__':
names=['hao','haohao','haort']
pool = Pool(processes=3)
pool.map(hello,names)
pool.close()
pool.join()
</pre>
线程
<pre>from multiprocessing.dummy import Pool
def hello(name):
print('hello ' + name)
if __name__ == '__main__':
names=['hao','haohao','haort']
pool = Pool(processes=3)
pool.map(hello,names)
pool.close()
pool.join()
</pre>
线程池和进程池导入包不一样,其他实现一样。只需要初始化池的大小,构造处理函数,以及处理列表,利用map方法即可,是不是很pythonic。
网友评论