这是我想要做的简化版本:
import numpyimport concurrent.futuressquares = numpy.empty((20,2))def make_square(i,squares): print('iteration',i) squares[i,0],squares[i,1] = i,i ** 2with concurrent.futures.ProcesspoolExecutor(2) as executor: for i in range(20): executor.submit(make_square,i,squares)
输出运行如下:
iteration 1iteration 0iteration 2iteration 3iteration 5iteration 4iteration 6iteration 7iteration 8iteration 9iteration 10iteration 11iteration 12iteration 13iteration 15iteration 14iteration 16iteration 17iteration 18iteration 19
很好地证明了该函数并发运行.但是正方形数组仍然是空的.
填充正方形数组的正确语法是什么?
其次,使用.map会更好吗?
提前致谢!
17年8月2日
哇.所以我徘徊在reddit-land,因为我不想接受这个问题.很高兴回到stackoverflow.谢谢@ilia w495 nikitin和@donkopotamus.这是我在reddit中发布的内容,它更详细地解释了这个问题的背景.
The posted code is an analogy of what I'm trying to do,which is populating a numpy array with a relatively simple calculation (dot product) involving two other arrays. The algorithm depends on a value N which can be anything from 1 on up,though we won't likely use a value larger than 24.I'm currently running the algorithm on a distributed computing system and the N = 20 versions take longer than 10 days to complete. I'm using doZens of cores to obtain the required memory,but gaining none of the benefits of multiple cpus. I've rewritten the code using numba which makes lower N variants superfast on my own laptop which can't handle the memory requirements for larger Ns,but alas,our distributed computing environment is not currently able to install numba. So I'm attempting concurrent.futures to take advantage of the multiple cpus in our computing environment in the hopes of speeding things up.
所以这不是时间密集的计算,而是1600万次迭代.初始化的数组是N x 2 ** N,即上述代码中的范围(16777216).
可能是因为通过多处理填充数组是不可能的.
解决方法 这里的问题是ProcesspoolExecutor将在单独的进程中执行一个函数.由于这些是单独的进程,具有单独的内存空间,因此您不能指望它们对数组(正方形)所做的任何更改将反映在父级中.因此,您的原始数组不会改变(正如您所发现的那样).
您需要执行以下任一 *** 作:
>使用ThreadPoolExecutor,但在一般情况下要注意,你仍然不应该尝试修改多个线程中的全局变量;
>重新编写代码,让您的进程/线程执行某种(昂贵的)计算并返回结果.
后者看起来像这样:
squares = numpy.zeros((20,2))def make_square(i): print('iteration',i) # compute expensive data here ... # return row number and the computed data return i,([i,i**2])with concurrent.futures.ProcesspoolExecutor(2) as executor: for row,result in executor.map(make_square,range(20)): squares[row] = result
这将产生您期望的结果:
[[ 0. 0.] [ 1. 1.] [ 2. 4.] ... [ 18. 324.] [ 19. 361.]]总结
以上是内存溢出为你收集整理的python-3.x – 通过concurrent.futures多处理填充numpy数组全部内容,希望文章能够帮你解决python-3.x – 通过concurrent.futures多处理填充numpy数组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)