2017-04-19 130 views
1

我需要添加兩個(A & B)的大隨機大小(10^7,10^12)的矢量與python或多處理中的兩個線程的幫助。然後需要將它存儲在C中。我還必須記錄我的代碼。最後需要從最終向量中找出最小和平均數。我嘗試了很多東西,目前正在使用Anaconda Jupyter筆記本。它接受代碼,但不給我任何輸出。線程/多處理/隊列?

這是我的代碼

"import time 
import multiprocessing 
import numpy as np 
import threading 
add_result = [] 
a = np.random.rand(10000000) 
b = np.random.rand(10000000) 
def calc_add(numbers): 
global add_results 
for n in numbers: 
    print('add' + str(a+b)) 
    add_result.append(a+b) 
    print('within a process result' +str(add_result)) 
    time.Time = start_time 
if __name__=="__main__": 
arr = a+b 
p1 = multiprocessing.Process(target = calc_add, args = (arr)) 
p2 = multiprocessing.Process(target = calc_add, args = (arr)) 
p1.start() 
p2.start() 
p1.join() 
p2.join() 

print("result" +str(add_result)) 
print("done!") 
+2

請把你的代碼放到你的問題中。 – martineau

回答

0

你不能做這樣的操作與多,因爲(在Python)過程是分開的,不共享彼此之間什麼。這意味着你的global變量在第二個進程p1中只是全局變量,這就是爲什麼你的add_result變量仍然等於「[]」。

請在您的問題中添加您的代碼,以便我們幫助您重新編寫它。

你還應該看看python的GIL以更好地理解爲什麼進程(和線程)不能幫助你完成任務。