类中的Python多处理池

时间:2016-07-21 18:35:36

标签: python class multiprocessing python-multiprocessing pool

我想在类的函数中运行多处理池作业:

from fuzzywuzzy import fuzz
from multiprocessing import Pool, cpu_count

class FindCloasestLocation(Resource):
    def __init__(self):
        self.score = 0.
        self.country = ''
        self.city = ''

    def CompareCities(self,location,row):
        print 'ss:',row
        score = self.ComparePhrases(location,row[10])
        return [score,row[5],row[10]]

    def CompareResults(self,inputs):
        score,country,city = inputs
        print 'sc:',score

        if score> self.score:
            self.score = score
            self.country = country
            self.city = city

    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('ws1', type=str, required=True, help="Word set 1 cannot be blank!", action='append')
        args = parser.parse_args()
        location = ' '.join(args['ws1'])
        print 'location:',location,'--',len(df)
        score = 0.
        country = ''
        city = ''
        if len(location)>1:
            try:
                pool = Pool(processes=cpu_count())
                for i in df.index:
                    pool.apply_async(self.CompareCities,(location,df.ix[i],), callback = self.CompareResults)
                pool.close()
                pool.join()
                return {'score':self.score,'country':self.country,'city':self.city}

            except:
                print 'none!'
                return None
        return None
    def ComparePhrases(self,p1,p2):
        return fuzz.ratio(p1,p2)

问题是没有调用函数CompareCities和CompareResults,并且得分值从未更新......任何想法?

0 个答案:

没有答案