Skip to content

Instantly share code, notes, and snippets.

@konradhafen
Created June 21, 2020 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save konradhafen/aa605c67bf798f07244bdc9d5d95ad12 to your computer and use it in GitHub Desktop.
Save konradhafen/aa605c67bf798f07244bdc9d5d95ad12 to your computer and use it in GitHub Desktop.
import multiprocessing as mp
import numpy as np
import time
def my_function(i, param1, param2, param3):
result = param1 ** 2 * param2 + param3
time.sleep(2)
return (i, result)
def get_result(result):
global results
results.append(result)
if __name__ == '__main__':
params = np.random.random((10, 3)) * 100.0
results = []
ts = time.time()
for i in range(0, params.shape[0]):
get_result(my_function(i, params[i, 0], params[i, 1], params[i, 2]))
print('Time in serial:', time.time() - ts)
print(results)
results = []
ts = time.time()
pool = mp.Pool(mp.cpu_count())
for i in range(0, params.shape[0]):
pool.apply_async(my_function, args=(i, params[i, 0], params[i, 1], params[i, 2]), callback=get_result)
pool.close()
pool.join()
print('Time in parallel:', time.time() - ts)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment