2012-02-14 55 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

相關問題