Skip to content

Instantly share code, notes, and snippets.

@artificialsoph
Last active January 22, 2017 19:38
Show Gist options
  • Save artificialsoph/2dad5ebed45c50543e10bdcb6f1224f1 to your computer and use it in GitHub Desktop.
Save artificialsoph/2dad5ebed45c50543e10bdcb6f1224f1 to your computer and use it in GitHub Desktop.
import multiprocessing
list_of_inputs = [0, 1, 2, 3]
def calculate_function(ind_input):
return ind_input**2
results = []
####### as a for loop (option 1, not parallel)
for ind_input in list_of_inputs:
results.append(calculate_function(ind_input))
####### as a starmap
num_cpu = 4 # use multiprocessing.cpu_count() to see how many are available
pool = multiprocessing.Pool(processes=num_cpu)
results = pool.map(calculate_function,list_of_inputs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment