具有两个数组作为输入的函数的Python Pool

时间:2014-11-05 19:26:16

标签: python numpy multiprocessing

我使用的函数f需要两个数组作为参数,在我的例子中,这些是坐标x,y的集合。我尝试两个使用游泳池

from multiprocessing import Pool
from module import f
import numpy as np


npoints = 2000 
x = np.linspace(0.1,2,npoints)
y = np.linspace(0,1,npoints)

xx,yy = np.meshgrid(x,y)

x=xx.flatten()
y=yy.flatten()

xy=np.array((x,y))

pool = Pool(8)

list = pool.apply_async(f,xy)
pool.close()
pool.join()

不幸的是,工作从未开始。甚至没有一个。出了什么问题?

谢谢

编辑:f可能看起来像

def f(x,y): 
return x*y

1 个答案:

答案 0 :(得分:0)

你的原始程序只在进程中启动,一个apply_async()是一个进程fork,你可以这样做:

from multiprocessing import Pool
import numpy as np
import os

def f(xcoor, ycoor):
    print "worker {}: coord {}".format(os.getpid(), (xcoor, ycoor))


npoints = 20
x = np.linspace(0.1,2,npoints)
y = np.linspace(0.1,2,npoints) #a typo in original program. 

xx,yy = np.meshgrid(x,y)

x=xx.flatten()
y=yy.flatten()

pool = Pool(8)
ll = []
for (xx, yy) in zip(x,y):
    ll.append(pool.apply_async(f, (xx,yy)))


pool.close()
pool.join()
# not in order
ll = [a.get() for a in ll]

输出(部分):

In [5]: run delme.py
worker 32489: coord (0.20000000000000001, 0.10000000000000001)
worker 32489: coord (0.40000000000000002, 0.10000000000000001)
worker 32490: coord (0.29999999999999999, 0.10000000000000001)
worker 32488: coord (0.10000000000000001, 0.10000000000000001)
worker 32489: coord (0.5, 0.10000000000000001)
worker 32490: coord (0.59999999999999998, 0.10000000000000001)
worker 32493: coord (0.69999999999999996, 0.10000000000000001)
worker 32493: coord (0.79999999999999993, 0.10000000000000001)
worker 32491: coord (0.89999999999999991, 0.10000000000000001)
worker 32488: coord (1.0999999999999999, 0.10000000000000001)
worker 32495: coord (0.99999999999999989, 0.10000000000000001)
worker 32488: coord (1.2, 0.10000000000000001)
worker 32489: coord (1.3, 0.10000000000000001)
worker 32490: coord (1.3999999999999999, 0.10000000000000001)
worker 32493: coord (1.5, 0.10000000000000001)
worker 32491: coord (1.5999999999999999, 0.10000000000000001)
worker 32492: coord (1.7, 0.10000000000000001)

我总是发现在处理numpy时,joblib包都是简单而强大的。你可以看看。它将多处理写入转换为单行+记忆