Advertisement
here2share

# rgb_multiprocessing_test.py

Apr 4th, 2021
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # rgb_multiprocessing_test.py
  2.  
  3. import multiprocessing as mp
  4. from random import randint as rnd
  5. import time
  6.  
  7.  
  8. def my_function(i, param1, param2, param3):
  9.     result = []
  10.     for z in range(800):
  11.         result += [tuple(rnd(0,255) for rgb in 'rgb')]
  12.     return (result)
  13.  
  14.  
  15. def get_result(result):
  16.     results.extend(result)
  17.  
  18.  
  19. if __name__ == '__main__':
  20.     params = [[[0,0,0] for i in range(1200)] for i in range(800)]
  21.     results = []
  22.     ts = time.time()
  23.     for i in range(0, len(params[0])):
  24.         get_result(my_function(i, params[0][i], params[1][i], params[2][i]))
  25.     print('Time in serial:', time.time() - ts)
  26.     #print(results)
  27.     results = []
  28.     ts = time.time()
  29.     pool = mp.Pool(mp.cpu_count())
  30.     for i in range(0, len(params[0])):
  31.         pool.apply_async(my_function, args=(i, params[0][i], params[1][i], params[2][i]), callback=get_result)
  32.     pool.close()
  33.     pool.join()
  34.     print('Time in parallel:', time.time() - ts)
  35.     #print(results)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement