2014-05-01 71 views
0

我想了解如何使用多處理,並設法讓下面的代碼工作。目標是通過設置n等於某個數字(現在爲100,因此前100個組合被測試)來處理CostlyFunction內的每個變量組合。我希望可以在每個進程返回其列表時操作w(CostlyFunction返回7個值的列表),並只將結果保留在給定範圍內。現在,w擁有所有100個列表,然後讓我操縱這些列表,但是當我使用n = 10MM時,w變得巨大且昂貴。有沒有一種方法可以評估CostlyFunction的輸出,因爲工人返回值然後「拋出」我不需要的值?Python多處理拋出基於以前值的結果

if __name__ == "__main__": 

    import csv 
    csvFile = open('C:\\Users\\bryan.j.weiner\\Desktop\\test.csv', 'w', newline='') 

    #width = -36000000/1000 
    #fronteir = [None]*1000 

    currtime = time() 
    n=100 
    po = Pool() 
    res = po.map_async(CostlyFunction,((i,) for i in range(n))) 
    w = res.get() 

    spamwriter = csv.writer(csvFile, delimiter=',') 
    spamwriter.writerows(w) 
    print(('2: parallel: time elapsed:', time() - currtime)) 

    csvFile.close() 
+0

也許我的問題是不是在w,但在如何我使用map_async。 CostlyFunction返回一個列表後,如果它滿足某些條件(比如返回列表的第6個元素位於所有返回列表的前10位),我只希望它將其添加到主列表中(或後面列出的任何列表) 。 – user3390169

+0

有人在嗎? – user3390169

回答

0

不幸的是,Pool沒有'過濾器'的方法;否則,在返回之前,您可能會修剪結果。 Pool.imap可能是處理內存問題的最佳解決方案:它將從CostlyFunction的結果中返回一個迭代器。

爲了對結果進行排序,我做了一個簡單的基於列表的類TopList,它存儲了固定數量的項目。根據關鍵功能,其所有項目均排名最高。

from collections import Userlist 

def keyfunc(a): 
    return a[5] # This would be the sixth item in a result from CostlyFunction 

class TopList(UserList): 
    def __init__(self, key, *args, cap=10): # cap is the largest number of results 
     super().__init__(*args)   # you want to store 
     self.cap = cap 
     self.key = key 
    def add(self, item): 
     self.data.append(item) 
     self.data.sort(key=self.key, reverse=True) 
     self.data.pop() 

這裏是你的代碼的外觀:

if __name__ == "__main__": 
    import csv 
    csvFile = open('C:\\Users\\bryan.j.weiner\\Desktop\\test.csv', 'w', newline='') 

    n = 100 
    currtime = time() 
    po = Pool() 
    best = TopList(keyfunc) 

    result_iter = po.imap(CostlyFunction, ((i,) for i in range(n))) 
    for result in result_iter: 
     best.add(result) 

    spamwriter = csv.writer(csvFile, delimiter=',') 
    spamwriter.writerows(w) 
    print(('2: parallel: time elapsed:', time() - currtime)) 

    csvFile.close()