2012-02-14 54 views
1

嘿我试着去运行一个简单的代码,使用并行的Python并行的Python全局名称错误

import sys, time 
import pp 
import numpy 
x = numpy.arange(-20.0,20.0,0.5) 
def function(raw_input): 
    f = 0 
    for i in numpy.arange(len(x)): 
     f+=1 
    a=raw_input[0] 
    b=raw_input[1] 
    c=raw_input[2] 
    d=raw_input[3] 
    print len(x) 
    return (a+b+c+d)+f 
# tuple of all parallel python servers to connect with 
ppservers =() 
#ppservers = ("10.0.0.1",) 

if len(sys.argv) > 1: 
    ncpus = int(sys.argv[1]) 
    # Creates jobserver with ncpus workers 
    job_server = pp.Server(ncpus, ppservers=ppservers) 
else: 
    # Creates jobserver with automatically detected number of workers 
    job_server = pp.Server(ppservers=ppservers) 
print "Starting pp with", job_server.get_ncpus(), "workers" 

start_time = time.time() 

# The following submits 4 jobs and then retrieves the results 
puts = ([1,2,3,4], [3,2,3,4],[4,2,3,6],[2,3,4,5]) 

jobs = [(raw_input, job_server.submit(function,(raw_input,),(), ("numpy",))) for raw_input in puts] 
for raw_input, job in jobs: 
    print "Sum of numbers", raw_input, "is", job() 

print "Time elapsed: ", time.time() - start_time, "s" 
job_server.print_stats() 

所以基本上我希望它添加[1,2,3同时增加号码列表一起,4]同时加入[3,2,3,4],[4,2,3,6],[2,3,4,5]。然后在所有答案中加上x(即80)的长度“f”。 失认沽应该是这样的:

开始第4名工人

号码[1,2,3,4]总和为90

号码[3,2,3的总和,图4是92

萨姆编号[4,2,3,6]是95

号码[2,3,4的点心,5]是94

经过时间:0.394000053406小号

工作执行统计:

工作计数|所有工作的百分比|工作时间总和|每项工作的时间|因为服务器创造就业机会服务器

 4 |  100.00 |  1.4380 |  0.359500 | local 

时间流逝0.442999839783

有问题即时是用“功能”之外的X壳回来与全局名称“X”没有定义,但如果你把X进入shell将返回完整的x数组。

我很困惑,为什么它的清晰定义足以让我回到“x”当我把它放在shell中,但作业没有找到x或函数定义之外的其他东西。

回答

1

我认为问题在于x是在作业服务器(它是在shell中运行的服务器上)定义的,但不是作业工作者,因为工作人员只能访问其输入中的变量。

当您提交作业时,您应该可以通过传入x作为附加参数来解决此问题。

据我所看到的,f的计算总是会有LEN的结果(X),所以你可以在价值简单地通过对于f来代替:

import sys, time 
import pp 
import numpy 
x = numpy.arange(-20.0,20.0,0.5) 
def function(raw_input,f): 
    a=raw_input[0] 
    b=raw_input[1] 
    c=raw_input[2] 
    d=raw_input[3] 
    return (a+b+c+d)+f 
# tuple of all parallel python servers to connect with 
ppservers =() 
#ppservers = ("10.0.0.1",) 

if len(sys.argv) > 1: 
    ncpus = int(sys.argv[1]) 
    # Creates jobserver with ncpus workers 
    job_server = pp.Server(ncpus, ppservers=ppservers) 
else: 
    # Creates jobserver with automatically detected number of workers 
    job_server = pp.Server(ppservers=ppservers) 
print "Starting pp with", job_server.get_ncpus(), "workers" 

start_time = time.time() 

# The following submits 4 jobs and then retrieves the results 
puts = ([1,2,3,4], [3,2,3,4],[4,2,3,6],[2,3,4,5]) 

jobs = [(raw_input, job_server.submit(function,(raw_input,len(x)),(), ("numpy",))) for raw_input in puts] 
for raw_input, job in jobs: 
    print "Sum of numbers", raw_input, "is", job() 

print "Time elapsed: ", time.time() - start_time, "s" 
job_server.print_stats() 
+0

感谢您的答复,我是意识到f将永远是相同的,我试图做一个简单的例子,将函数外部的变量拉入计算中。真正的目标实际上是一个不同的代码,其中包含类似这样的内容,并且它具有在每次迭代时都会改变的变量,并且并行作业内部的计算应该追加到也在函数之外的数组中,所以这就是为什么我没有不要把x放在里面,这可能会起作用,但是如果我可以创建一个数组或者我认为的参数的列表部分。谢谢您的帮助 – user991926 2012-02-15 13:05:03

相关问题