CPU密集型process pool,io密集型thread pool
from multiprocessing import cpu_count, Pool
with Pool(max(cpu_count() - 1, 1)) as pool:
result = http://pool.map(your_function, param_list)
GIL锁的麻烦
GIL的存在更是带来更多并行问题。
对于Pytorch的干扰:
- GIL 的存在导致 nn.DataParallel 性能垃圾
- GIL 导致多线程的 dataloader 没有太大意义
这两个问题,目前 pytorch 都是用 multiprocessing 解决的, 但是随之而来的就是内存占用, context switch, IPC, 和 fork-safety 相关的无数的坑. 毕竟 fork is evil.