Advertisement
Guest User

Untitled

a guest
Sep 10th, 2021
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. from multiprocessing.pool import ThreadPool
  2. from multiprocessing import Pool
  3. import concurrent.futures
  4.  
  5. import sys
  6. import time
  7.  
  8.  
  9. def testFunc(idx):
  10.     x = 0
  11.  
  12.     # Some Math
  13.     for i in range(100000000):
  14.         x += idx + i
  15.         x /= 1.5
  16.  
  17.     return x
  18.  
  19.  
  20. def run():
  21.  
  22.     # My Machine has 10 Cores and 20 Logical Processors. I set 15 Processes
  23.     # pool = ThreadPool(processes=15)  # Per 1 thread
  24.     pool = Pool(processes=15)  # Per all threads
  25.  
  26.     def callbackFunc(args):
  27.         print('Result: ' + str(args))
  28.  
  29.     for i in range(50):
  30.         pool.apply_async(testFunc, args=(i,), callback=callbackFunc)
  31.  
  32.     pool.close()
  33.     pool.join()
  34.  
  35.  
  36. if __name__ == '__main__':
  37.  
  38.     start = time.time()
  39.  
  40.     run()
  41.  
  42.     end = time.time()
  43.     print('Time: ' + str(end - start))
  44.  
  45.     sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement