Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Created January 8, 2022 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonymorast/2cac7f80bc85d7c14a09624be379776f to your computer and use it in GitHub Desktop.
Save anthonymorast/2cac7f80bc85d7c14a09624be379776f to your computer and use it in GitHub Desktop.
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