Python多进程相关的坑

Python的multiprocessing模块实现了多进程功能,但官方文档上只有一些比较简单的用法,主要是使用函数作为process的target,而如何在class中使用多进程并没有多讲解。google出两篇比较详细的文章,建议从它们入门:

https://pymotw.com/2/multiprocessing/basics.html

https://pymotw.com/2/multiprocessing/communication.html

 

下面记录一下自己这周在python多进程上碰到的坑:

创建进程时,参数必须能够被pickle,所以有些自定义的类对象实例是不能被作为参数的

和threading不同,multiprocessing Process参数必须能够被pickle进行序列化

image

Python 2.7,Can’t pickle <type ‘instancemethod’>

python 2.7 的 python 3.5版本中,multiprocessing的行为是不同的,有些代码可以在3.5中运行,在2.7中却运行出错

例如上,在3.5中可以运行,这是因为在3.5版本中,pick可以序列化更多的类型。

尽量避免类实例中包含multiprocess.Manager实例,否则会有

TypeError: Pickling an AuthenticationString object is disallowed for security reasons

或者:

_pickle.PicklingError: Can't pickle <class 'weakref'>: attribute lookup weakref on builtins failed

进程间共享的对象,使用Manager进行管理

Manager会生成一个进程,所以不同进程间访问统一变量,是通过IPC进行的,会有性能上的开销。

 

关于主进程所在的文件代码

使用multiprocessing时,主模块会被import到各进程中,所以创建子进程的部分,必须使用

if __name__ == '__main__:

进行保护,否则会有runtime error,或者递归创建子进程

 

Update 1 (2018-11-08): 

在Windows环境中,jupyter-notebook中,即使使用if __name__ == '__main__进行保护,也会出现runtime error,这个时候可以将jupyter中的代码下载成py脚本,直接运行脚本。

作为对比,Linux下运行的jupyter-notebook并不会。

posted @ 2016-09-03 19:38  li.dp  阅读(11233)  评论(2编辑  收藏  举报