尝试使用多重处理在python中填充数组 [英] Trying to use multiprocessing to fill an array in python
本文介绍了尝试使用多重处理在python中填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码
x = 3;
y = 3;
z = 10;
ar = np.zeros((x,y,z))
from multiprocessing import Process, Pool
para = []
process = []
def local_func(section):
print "section %s" % str(section)
ar[2,2,section] = 255
print "value set %d", ar[2,2,section]
pool = Pool(1)
run_list = range(0,10)
list_of_results = pool.map(local_func, run_list)
print ar
ar中的值未通过多线程更改,这可能是什么问题?
The value in ar was not changed with multithreading, what might be wrong?
谢谢
推荐答案
您在这里使用的是多个进程,而不是多个线程.因此,每个local_func
实例都获得其自己的ar
副本.您可以使用自定义Manager
来创建共享的numpy数组,您可以将其传递给每个子进程并获得期望的结果:
You're using multiple processes here, not multiple threads. Because of that, each instance of local_func
gets its own separate copy of ar
. You can use a custom Manager
to create a shared numpy array, which you can pass to each child process and get the results you expect:
import numpy as np
from functools import partial
from multiprocessing import Process, Pool
import multiprocessing.managers
x = 3;
y = 3;
z = 10;
class MyManager(multiprocessing.managers.BaseManager):
pass
MyManager.register('np_zeros', np.zeros, multiprocessing.managers.ArrayProxy)
para = []
process = []
def local_func(ar, section):
print "section %s" % str(section)
ar[2,2,section] = 255
print "value set %d", ar[2,2,section]
if __name__ == "__main__":
m = MyManager()
m.start()
ar = m.np_zeros((x,y,z))
pool = Pool(1)
run_list = range(0,10)
func = partial(local_func, ar)
list_of_results = pool.map(func, run_list)
print ar
这篇关于尝试使用多重处理在python中填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文