new*_*bie 2 python multiprocessing python-3.x python-multiprocessing
我最近开始在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')
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
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
Run Code Online (Sandbox Code Playgroud)
结果列表显示为空.有没有办法让所有这些进程共享一个公共变量来存储数据.我们如何使用多处理实现此功能?
进程不共享内存.因此您需要使用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')
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |