【Python】Python 多核并行计算

原文转自:https://abcdabcd987.com/python-multiprocessing/

 

Python 多核并行计算

Nov 2, 2016 • python

以前写点小程序其实根本不在乎并行,单核跑跑也没什么问题,而且我的电脑也只有双核四个超线程(下面就统称好了),觉得去折腾并行没啥意义(除非在做IO密集型任务)。然后自从用上了32核128GB内存,看到 htop 里面一堆空载的核,很自然地就会想这个并行必须去折腾一下。后面发现,其实 Python 的并行真的非常简单。

htop 32cores 128GB RAM

multiprocessing vs threading

Python 自带的库又全又好用,这是我特别喜欢 Python 的原因之一。Python 里面有 multiprocessing和 threading 这两个用来实现并行的库。用线程应该是很自然的想法,毕竟(直觉上)开销小,还有共享内存的福利,而且在其他语言里面线程用的确实是非常频繁。然而,我可以很负责任的说,如果你用的是 CPython 实现,那么用了 threading 就等同于和并行计算说再见了(实际上,甚至会比单线程更慢),除非这是个IO密集型的任务。

GIL

CPython 指的是 python.org 提供的 Python 实现。是的,Python 是一门语言,它有各种不同的实现,比如 PyPyJythonIronPython 等等……我们用的最多的就是 CPython,它几乎就和 Python 画上了等号。

CPython 的实现中,使用了 GIL 即全局锁,来简化解释器的实现,使得解释器每次只执行一个线程中的字节码。也就是说,除非是在等待IO操作,否则 CPython 的多线程就是彻底的谎言!

有关 GIL 下面两个资料写的挺好的:

multiprocessing.Pool

因为 GIL 的缘故 threading 不能用,那么我们就好好研究研究 multiprocessing。(当然,如果你说你不用 CPython,没有 GIL 的问题,那也是极佳的。)

首先介绍一个简单粗暴,非常实用的工具,就是 multiprocessing.Pool。如果你的任务能用 ys = map(f, xs) 来解决,大家可能都知道,这样的形式天生就是最容易并行的,那么在 Python 里面并行计算这个任务真是再简单不过了。举个例子,把每个数都平方:


 
 
  1. import multiprocessing
  2. def f (x):
  3. return x * x
  4. cores = multiprocessing .cpu_count()
  5. pool = multiprocessing .Pool(processes =cores)
  6. xs = range( 5)
  7. # method 1: map
  8. print pool . map(f, xs) # prints [0, 1, 4, 9, 16]
  9. # method 2: imap
  10. for y in pool .imap(f, xs):
  11. print y # 0, 1, 4, 9, 16, respectively
  12. # method 3: imap_unordered
  13. for y in pool .imap_unordered(f, xs):
  14. print(y) # may be in any order

map 直接返回列表,而 i 开头的两个函数返回的是迭代器;imap_unordered 返回的是无序的。

当计算时间比较长的时候,我们可能想要加上一个进度条,这个时候 i 系列的好处就体现出来了。另外,有一个小技巧,就是输出 \r 可以使得光标回到行首而不换行,这样就可以制作简易的进度条了。


 
 
  1. cnt = 0
  2. for _ in pool .imap_unordered(f, xs):
  3. sys .stdout . write( 'done % d/ % d \r ' % (cnt, len(xs)))
  4. cnt += 1

更复杂的操作

要进行更复杂的操作,可以直接使用 multiprocessing.Process 对象。要在进程间通信可以使用:

其中我强烈推荐的就是 Queue,因为其实很多场景就是生产者消费者模型,这个时候用 Queue 就解决问题了。用的方法也很简单,现在父进程创建 Queue,然后把它当做 args 或者 kwargs 传给 Process 就好了。

使用 Theano 或者 Tensorflow 等工具时的注意事项

需要注意的是,在 import theano 或者 import tensorflow 等调用了 Cuda 的工具的时候会产生一些副作用,这些副作用会原样拷贝到子进程中,然后就发生错误,如:

could not retrieve CUDA device count: CUDA_ERROR_NOT_INITIALIZED

 
 

解决的方法是,保证父进程不引入这些工具,而是在子进程创建好了以后,让子进程各自引入。

如果使用 Process,那就在 target 函数里面 import。举个例子:


 
 
  1. import multiprocessing
  2. def hello (taskq, resultq):
  3. import tensorflow as tf
  4. config = tf .ConfigProto()
  5. config .gpu_options .allow_growth = True
  6. sess = tf .Session(config =config)
  7. while True:
  8. name = taskq .get()
  9. res = sess .run(tf .constant( 'hello ' + name))
  10. resultq .put(res)
  11. if __name__ == '__main__':
  12. taskq = multiprocessing .Queue()
  13. resultq = multiprocessing .Queue()
  14. p = multiprocessing .Process(target =hello, args =(taskq, resultq))
  15. p .start()
  16. taskq .put( 'world')
  17. taskq .put( 'abcdabcd987')
  18. taskq .close()
  19. print(resultq .get())
  20. print(resultq .get())
  21. p .terminate()
  22. p .join()

如果使用 Pool,那么可以编写一个函数,在这个函数里面 import,并且把这个函数作为 initializer传入到 Pool 的构造函数里面。举个例子:


 
 
  1. import multiprocessing
  2. def init ():
  3. global tf
  4. global sess
  5. import tensorflow as tf
  6. config = tf .ConfigProto()
  7. config .gpu_options .allow_growth = True
  8. sess = tf .Session(config =config)
  9. def hello (name):
  10. return sess .run(tf .constant( 'hello ' + name))
  11. if __name__ == '__main__':
  12. pool = multiprocessing .Pool(processes = 2, initializer =init)
  13. xs = [ 'world', 'abcdabcd987', 'Lequn Chen']
  14. print pool . map(hello, xs)

Pool.apply_async:传入不确定参数

 


 
 
  1. import multiprocessing as mp
  2. import time
  3. def foo_pool(x):
  4. time.sleep( 2)
  5. return x*x
  6. result_list = []
  7. def log_result(result):
  8. # This is called whenever foo_pool(i) returns a result.
  9. # result_list is modified only by the main process, not the pool workers.
  10. result_list.append(result)
  11. def apply_async_with_callback():
  12. pool = mp.Pool()
  13. for i in range( 10):
  14. pool.apply_async(foo_pool, args = (i, ), callback = log_result)
  15. pool.close()
  16. pool.join()
  17. print(result_list)
  18. if __name__ == '__main__':
  19. apply_async_with_callback()

参考:http://blog.csdn.net/xluren/article/details/46861621

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值