Python: Windows和Linux下调用multiprocessing库的学习笔记
多线程与多进程的基本概念和优劣以及适用场合可以参见:
https://blog.csdn.net/linraise/article/details/12979473
总结得比较详细,我只在这里做一些测试。
multiprocessing的执行在Windows和Linux下有诸多不同,具体细节和原理可以参考官方说明文件:
https://docs.python.org/2/library/multiprocessing.html
Windows下持续运行没有输出的情况
如官方文件所说,在Windows下调用multiprocessing库必须要将Pool或者其它实际运行的代码放到:
if __name__ == '__main__':
之下。否则在子进程中将会复制主进程中的所有内容,并不断循环创建新的子进程,从而造成死循环。
Windows下设置全局变量
直接上测试代码:
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Pool
from numpy import *
h = 1
def hhh(i):
print('t')
global h
h += 2
return h
if __name__ == '__main__':
for i in range(4):
pool = Pool(2)
a = pool.map(hhh,(arange(6)))
pool.close()
pool.join()
print(a)
print(h)
运行结果为:
[3, 5, 7, 9, 11, 13]
[3, 5, 3, 7, 9, 5]
[3, 3, 5, 5, 7, 7]
[3, 5, 7, 9, 11, 13]
1
我们可以看到 1)此时使用多进程对全局变量h进行更改,并不会对主进程中h的值造成影响,而会导致子进程中的h值出现资源竞争,导致每次运行结果中h的值可能不同; 2)子程序中的运行结果不会打印到屏幕上
第二点是Python IDLE(Integrated Development and Learning Environment)的问题,具体解释可以参看一个stackflow上的回答:
https://stackoverflow.com/questions/23480498/why-doesnt-print-work-in-python-multiprocessing-pool-map
下面我们尝试使用多线程:
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import Pool
from numpy import *
h = 1
def hhh(i):
print('t')
global h
h += 2
return h
if __name__ ==