Created
November 16, 2012 09:12
-
-
Save khinsen/4085740 to your computer and use it in GitHub Desktop.
Solution modèle pour la parallélisation du comptage des votes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Scientific.DistributedComputing.MasterSlave \ | |
import MasterProcess, SlaveProcess, runJob | |
votes = 20*['A'] + 25*['B'] + 22*['C'] | |
chunk_size = 10 | |
class Master(MasterProcess): | |
def run(self): | |
index = 0 | |
ntasks = 0 | |
while index < len(votes): | |
self.requestTask("count", votes[index:index+chunk_size]) | |
index += chunk_size | |
ntasks += 1 | |
total = {} | |
for i in range(ntasks): | |
task_id, tag, subcounts = self.retrieveResult("count") | |
for name, count in subcounts.items(): | |
total[name] = total.get(name, 0) + count | |
print total | |
class VoteCounter(SlaveProcess): | |
def do_count(self, votes): | |
counts = {} | |
for v in votes: | |
counts[v] = counts.get(v, 0) + 1 | |
return counts | |
runJob("count_votes", Master, VoteCounter, | |
launch_slaves = 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment