Advertisement
here2share

# process_module_timeit.py

Mar 12th, 2021
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. # process_module_timeit.py
  2.  
  3. import datetime
  4. import random
  5. import time
  6. from multiprocessing import Process, Manager
  7.  
  8. def test_f(process_num, CPUs, test_d):
  9.     share = 100000.0 / CPUs
  10.     ctr=0
  11.     while ctr < share:
  12.         ctr += 1
  13.     test_d["data %d" % process_num] = ctr, str(random.randint(0,99999999)).zfill(8)
  14.  
  15. if __name__ == '__main__':
  16.     ## define the dictionary to be used to communicate
  17.     manager = Manager()
  18.     test_d = manager.dict()
  19.  
  20.     ## start processes
  21.     process_list=[]
  22.     print '...'
  23.     for CPUs in range(1,17):
  24.         t = time.clock()
  25.         for ctr in range(CPUs):
  26.             p = Process(target=test_f, args=(ctr, CPUs, test_d,))
  27.             p.start()
  28.             process_list.append(p)
  29.  
  30.         alive=True
  31.         while alive:     ## wait for all processes to finish
  32.             alive=False
  33.             for p in process_list:
  34.                 if p.is_alive():
  35.                     alive=True
  36.  
  37.         for ctr in range(CPUs):
  38.             process_list[ctr].join()
  39.             print("process number: %s " % str(ctr+1).zfill(3) + str(test_d["data %d"%ctr]))
  40.             stop = time.clock()
  41.         print('CPUs*: %d  time: %f' % (CPUs, stop-t))
  42.         print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement