특정 사용자에게 파일에 대한 권장 사항을 pickle 처리하는 getRecommendations 메소드를 호출하려고합니다. 나는 작동하는 책의 코드를 사용했다. 그러나 단 하나의 코어 만 작동한다는 것을 알았습니다. 그리고 훨씬 빠른 것이기 때문에 모든 코어가 작동하도록하고 싶습니다.통신이없는 파이썬 멀티 코어
다음은이 방법입니다. 당신은 내가 모든 고객에게 추천을 만들려고 볼 수
def getRecommendations(prefs,person,similarity=sim_pearson):
print "working on recommendation"
totals={}
simSums={}
for other in prefs:
# don't compare me to myself
if other==person: continue
sim=similarity(prefs,person,other)
# ignore scores of zero or lower
if sim<=0: continue
for item in prefs[other]:
# only score movies I haven't seen yet
if item not in prefs[person] or prefs[person][item]==0:
# Similarity * Score
totals.setdefault(item,0)
totals[item]+=prefs[other][item]*sim
# Sum of similarities
simSums.setdefault(item,0)
simSums[item]+=sim
# Create the normalized list
rankings=[(total/simSums[item],item) for item,total in totals.items()]
# Return the sorted list
rankings.sort()
rankings.reverse()
ranking_output = open("data/rankings/"+str(int(person))+".ranking.recommendations","wb")
pickle.dump(rankings,ranking_output)
return rankings
는 그것은
for i in customerID:
print "working on ", int(i)
#Make this working with multiple CPU's
getRecommendations(pickle.load(open("data/critics.recommendations", "r")), int(i))
를 통해이라고합니다. 나중에 사용됩니다.
어떻게이 방법을 다중 처리 할 수 있습니까? 이 때문에, 당신은 단지 'i'를 getRecommendations에 합격 가정한다 (
from multiprocessing import Pool
NUMBER_OF_PROCS = 5 # some number... not necessarily the number of cores due to I/O
pool = Pool(NUMBER_OF_PROCS)
for i in customerID:
pool.apply_async(getRecommendations, [i])
pool.close()
pool.join()
: 나는 몇 가지 예 또는 같은 심지어 documentation
for stand pool = Pool (4) 나는이 방법에 대해 비평가를 제공해야합니다. 내가 severals 물건을 위해 그것을 사용하고 싶어하기 때문에. –
확실히 풀에 대한 설명서를 읽으면 도움이 될 것입니다 :) (다중 처리를 사용하여 원하는 작업을 수행 할 수있는 다른 방법이 많이 있습니다.) –