Created
June 21, 2020 02:44
-
-
Save konradhafen/aa605c67bf798f07244bdc9d5d95ad12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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