2013-08-28 2 views
0

특정 사용자에게 파일에 대한 권장 사항을 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

답변

0

당신이 원하는 무엇인가 (약, 검증되지 않은)을 읽어 그것을 얻을하지 않습니다 pickle.load는 한 번만 수행해야합니다.)

+0

for stand pool = Pool (4) 나는이 방법에 대해 비평가를 제공해야합니다. 내가 severals 물건을 위해 그것을 사용하고 싶어하기 때문에. –

+0

확실히 풀에 대한 설명서를 읽으면 도움이 될 것입니다 :) (다중 처리를 사용하여 원하는 작업을 수행 할 수있는 다른 방법이 많이 있습니다.) –

0

제임스가 준 답변이 정확한 것입니다. 나는 단지를 추가 할거야, 당신은

from multiprocessing import Pool 

그리고를 통해 멀티 프로세싱 모듈을 가져와야합니다, 풀 (4) 병렬로 작동 4 "노동자"프로세스를 만들 것을 의미한다 당신의 작업을 수행합니다.

+0

어떤 방식 으로든 실제 개수의 코어를 사용할 수 있습니까? 풀 (numberOfCores-1)이라고 말할 수 있습니까? –

+0

http://docs.python.org/2/library/multiprocessing.html#multiprocessing.cpu_count – user2719952