2014-05-01 18 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()