并行Python全局名称错误

时间:2012-02-14 19:18:13

标签: python parallel-processing

嘿我试图运行一个简单的代码,使用Parallel 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,2,4]的同时添加[1,2,3,4],[4,2,3,4],[4,2,3,6],[2,3] ,4,5]。并为所有答案添加“f”,即x的长度(即80)。 输出应该是这样的:

以4名工人开始pp

数字之和[1,2,3,4]为90

数字之和[3,2,3,4]为92

数字之和[4,2,3,6]为95

数字之和[2,3,4,5]为94

已过去的时间:0.394000053406 s

工作执行统计:

工作计数|所有工作的百分比|工作时间总和|每个工作的时间|工作服务器

     4 |        100.00 |       1.4380 |     0.359500 | local

自服务器创建以来经过的时间0.442999839783

我遇到的问题是,在“函数”之外的x,shell返回时未定义全局名称“x”,但如果将x放入shell,则返回完整的x数组。

我很困惑为什么它明确定义足以让我回到“x”当我把它放在shell中但是作业找不到x或函数定义之外的任何其他东西。

1 个答案:

答案 0 :(得分: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()