Skip to content

Commit

Permalink
Use map and views to streamline the workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mlouhivu committed Nov 20, 2018
1 parent c5ef96c commit 4224763
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions multiprocessing/work-distribution/solution/pool-of-workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ def average(chunk):
# split into tasks
tasks = []
for i in range(0, len(pdb), n):
chunk = pdb.coordinates[i:i+n].copy()
chunk = pdb.coordinates[i:i+n]
tasks.append(chunk)

# submit each task to the pool
results = []
for chunk in tasks:
res = pool.apply_async(average, [chunk])
results.append(res)
# submit tasks to the pool
result = pool.map(average, tasks)

# wait for all tasks to finish
pool.close()
Expand All @@ -33,14 +30,13 @@ def average(chunk):
# collect results
averages = []
weights = []
for res in results:
avg, w = res.get()
for avg, w in result:
averages.append(avg)
weights.append(w)
averages = array(averages)
weights = array(weights, ndmin=2)

# calculate the center of coordinates
origo = sum(averages * weights.T) / len(pdb)
print origo
print(origo)

0 comments on commit 4224763

Please sign in to comment.