2011-12-15 85 views
0

我最近想開發一個HTTP服務器邊界來包裝我的ipcontroller/ipengine集羣程序。該服務器是從BaseHTTPServer派生的簡單服務器。當服務器收到HTTP Get請求時,其do_GET方法將調用幾個mec.execute()方法來完成作業。這是代碼示例。IPython並行計算中的同步問題

do_GET 
{ 
b = parameter 
mec.scatter("a", b) 
mec.execute("c=fun(a)") 
d = mec.gather("c") 
write d 
} 

我會在語句mec.execute(「c = fun(a)」)上面對同步問題嗎?從我的猜測來看,每個ipengie將創建一個變量「c」,其值爲「fun(a)」。如果兩個線程同時使用不同的參數調用do_Get方法,那麼每個ipengine的「c」值是多少。

回答

1

如果你能表達的任務,因爲單個並行函數調用,那你應該是安全的,因爲沒有其他請求可以潛入之間(和發動機全局不必觸及),例如:

from IPython import parallel 

rc = parallel.Client() 
view = rc[:] 

@view.parallel(block=True) 
def pfun(a): 
    """each engine will get a chunk of a, not the whole thing""" 
    c = fun(a) 
    return c 

# a will be scattered and c will be gathered 
c = pfun(a) 

但如果不是這樣,那麼最簡單的解決方案可能是通過爲給定請求的變量提供具有UUID的唯一後綴來確保跨作業的名稱衝突:

import uuid 
suffix = str(uuid.uuid4()).replace('-','') # remove '-' so we have a valid identifier 
a_name = "a_" + suffix 
c_name = "c_" + suffix 
mec.scatter(a_name, b) 
mec.execute("%s = fun(%s)" % (c_name, a_name)) 
d = mec.gather(c_name)