我最近开始在python中使用多处理,我有以下代码来更新多个进程的列表项。但它给出了空列表。
from multiprocessing import Pool
import time
global_list = list()
def testfun(n):
print('started ', n)
time.sleep(1)
global_list.append(n)
print('completed ', n)
def call_multiprocessing_function():
mytasks = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
with Pool() as pool:
pool.map(testfun, mytasks)
if __name__ == "__main__":
print('starting the script')
print(global_list)
call_multiprocessing_function()
print(global_list)
print('completed the script')
我收到以下输出
starting the script
[]
started a
started b
started c
started d
completed a
started e
completed b
started f
completed c
started g
completed d
started h
completed e
started i
completed f
started j
completed g
started k
completed h
started l
completed i
started m
completed j
started n
completed k
completed l
completed m
completed n
[]
completed the script
结果列表显示为空。有没有办法让所有这些进程共享一个公共变量来存储数据。我们如何使用多处理实现此功能?
答案 0 :(得分:5)
进程不共享内存。所以你需要使用Manager.list
import time
from multiprocessing import Pool, Manager
m=Manager()
global_list = m.list()
def testfun(n):
print('started ', n)
time.sleep(1)
global_list.append(n)
print('completed ', n)
def call_multiprocessing_function():
mytasks = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
p=Pool()
p.map(testfun, mytasks)
if __name__ == "__main__":
print('starting the script')
print(global_list)
call_multiprocessing_function()
print(global_list)
print('completed the script')
输出:
starting the script
[]
started a
started b
started c
started d
started e
started f
started g
started h
completed e
started i
completed f
started j
completed d
started k
completed a
started l
completed g
started m
completed b
completed c
started n
completed h
completed i
completed j
completed k
completed l
completed n
completed m
['e', 'f', 'd', 'a', 'g', 'b', 'c', 'h', 'i', 'j', 'k', 'l', 'n', 'm']
completed the script