Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # heavy_multiprocessing.py
- import multiprocessing as mp
- from random import random as rnd
- import time
- def my_function(i, param1, param2, param3):
- result = 0
- for z in range(5000000):
- result += param1 ** 2 * param2 + param3
- return (i, result)
- def get_result(result):
- global results
- results.append(result)
- if __name__ == '__main__':
- params = [[(rnd()) for i in range(10)] for j in range(3)]
- results = []
- ts = time.time()
- for i in range(0, len(params[0])):
- get_result(my_function(i, params[0][i], params[1][i], params[2][i]))
- print('Time in serial:', time.time() - ts)
- print(results)
- print
- results = []
- ts = time.time()
- pool = mp.Pool(mp.cpu_count())
- for i in range(0, len(params[0])):
- pool.apply_async(my_function, args=(i, params[0][i], params[1][i], params[2][i]), callback=get_result)
- pool.close()
- pool.join()
- print('Time in parallel:', time.time() - ts)
- print(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement