在多进程环境下,主进程启动多个子进程,其中某个子进程又启动了多个子进程。此方案在执行时遇到了问题,即无法启动嵌套的进程组。
以下是关键代码段:
# parallel_test.py
import Queue
import multiprocessing
import time
import threading
def worker(q):
while not q.empty():
try:
row = q.get(False)
print row
time.sleep(1)
except Queue.Empty:
break
def main():
print 'creating queue'
q = multiprocessing.Queue()
print 'enqueuing'
for i in range(100):
q.put(i)
num_processes = 15
pool = []
for i in range(num_processes):
print 'launching process {0}'.format(i)
p = multiprocessing.Process(target=worker, args=(q,))
p.start()
pool.append(p)
for p in pool:
p.join()
if __name__ == '__main__':
main()
# main_loop_test.py
import parallel_test
from multiprocessing import Pool
import time
def main():
targets = [parallel_test.main]
running = True
while running:
try:
p = Pool(12)
for target in targets:
p.apply_async(target)
p.close() # For some reason you need to run close() before join()
p.join() # What for all the steps to be done
print 'All steps done'
time.sleep(2)
except KeyboardInterrupt as e:
print "<<<<<<<<<<<<<<<<<<CAUGHT KEYBOARD INTERRUPT FROM USER>>>>>>>>>>>>>>>>>>>"
running = False
if __name__ == '__main__':
main()
单独运行parallel_test.py时,程序运行正常,并按预期打印数字。但是,当从另一个文件作为另一个进程启动时,程序会出现问题。parallel_test.py似乎尝试启动一个进程(该进程什么也不做),然后退出函数,main_loop_test.py打印“All steps done”。没有打印数字。程序输出如下:
creating queue
enqueuing
launching process 0
All steps done
creating queue
enqueuing
launching process 0
All steps done
2、解决方案
要解决此问题,有两种方法:
1. 将进程设置为非守护进程
在parallel_test中创建的进程是守护进程,守护进程不允许创建子进程。因此,需要将进程设置为非守护进程。可以在创建进程时将进程的daemon属性设置为False。
p = multiprocessing.Process(target=test.main)
p.daemon = False
p.start()
p.join()
2. 使用支持嵌套并行的多处理库
使用支持嵌套并行的多处理库,如pathos。pathos是一个multiprocessing的派生库,它提供了更简单的使用方法。
from pathos.multiprocessing import ProcessingPool as PPool
# 创建一个非阻塞的处理池映射(即 async_map)
amap = PPool().amap
# 定义一个“内部”函数
def g(x):
import random
return int(x * random.random())
# 定义一个“外部”函数
def f(x,y):
return x*y
# 定义两个不同长度的列表
x = range(10)
y = range(5)
# 在嵌套并行中进行评估
res1 = amap(f, [h(x),h(x),h(x),h(x),h(x)], y)
res2 = amap(f, [h(x),h(x),h(x),h(x),h(x)], y)
res3 = amap(f, [h(x),h(x),h(x),h(x),h(x)], y)
res4 = amap(f, [h(x),h(x),h(x),h(x),h(x)], y)
res1.get()
res2.get()
res3.get()
res4.get()