Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Created Jan 8, 2022
Embed
What would you like to do?
processes = 20 # using 20 processes since I'm running this on my server
with Pool(processes) as p: # create the pool
print(f"Determining square values.")
# spawn the processes for each Latin square (lines is a list of strings representing the squares)
results = p.map(get_value, lines)
print(f"Aggregating results.")
# determine how to split up the resulting (square, value) pairs [i.e. how many per process]
values_per_list = int(len(results)/processes)
extra = len(results) - (values_per_list*processes)
result_lists = []
# break the list up into smaller lists for each process
for i in range(processes-1):
result_lists.append(results[(values_per_list*i):(values_per_list*(i+1))])
# give the last process all of the remaining squares
result_lists.append(results[(processes-1)*values_per_list:])
# get all of the dictionaries returned by the build_dict function, this will be a list of dictionaries
union_dicts = p.map(build_dict, result_lists)
# combine each dictionary into one dictionary, namely 'totals'
for d in union_dicts:
totals = {**totals, **d}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment