我有大約4000個數據點,我有一個處理它們的程序。由於數量巨大,程序非常慢,儘管我在嵌套循環中使用了numpy.arange進行一些向量化。Python中多處理多參數方法
我搜索了pool.map,問題是它只有一個參數。我看到這裏存在一些對此問題的解答,Python multiprocessing pool.map for multiple arguments。我使用了最後一個使用map方法的參數列表,我有大約4個參數,我把它們放在一個列表中,並在map方法中傳入一個長的函數名。在函數中,我從列表中提取每個參數並執行操作,但它不起作用。這就是我所說的地圖代碼,
if __name__ == '__main__':
pool= Pool(processes=8)
p= pool.map (kriging1D, [x,v,a,n])
plt.scatter(x,v,color='red')
plt.plot(range(5227),p,color='blue')
這是要並行化功能,
def kriging1D(args):
x=args[0]
v=args [1]
a= args [2]
n= args [3]
#perform some operations on the args..
...
#return the result..
但是,我得到這個錯誤,
plt.plot(range(5227),p,color='blue')
NameError: name 'p' is not defined
注:添加此之前,線,
if __name__ == '__main__':
我懂了錯誤,
RuntimeError:
Attempt to start a new process before the current process
has finished its bootstrapping phase.
This probably means that you are on Windows and you have
forgotten to use the proper idiom in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce a Windows executable.
這就是爲什麼我添加的if語句。
欲瞭解更多清晰度:v和x是每個4000的大尺寸(均具有相同長度)的向量。我的意圖是並行化每個v [i]和x [i]的處理,例如一次處理多個v和x元素,而不是逐個處理元素。
任何人都可以告訴我我在做什麼錯誤嗎?或者,建議一種替代方法?
謝謝。
[Python多處理pool.map多個參數]的可能重複(http://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments) –
謝謝,我會檢查。 – Dania