Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # process_module_timeit.py
- import datetime
- import random
- import time
- from multiprocessing import Process, Manager
- def test_f(process_num, CPUs, test_d):
- share = 100000.0 / CPUs
- ctr=0
- while ctr < share:
- ctr += 1
- test_d["data %d" % process_num] = ctr, str(random.randint(0,99999999)).zfill(8)
- if __name__ == '__main__':
- ## define the dictionary to be used to communicate
- manager = Manager()
- test_d = manager.dict()
- ## start processes
- process_list=[]
- print '...'
- for CPUs in range(1,17):
- t = time.clock()
- for ctr in range(CPUs):
- p = Process(target=test_f, args=(ctr, CPUs, test_d,))
- p.start()
- process_list.append(p)
- alive=True
- while alive: ## wait for all processes to finish
- alive=False
- for p in process_list:
- if p.is_alive():
- alive=True
- for ctr in range(CPUs):
- process_list[ctr].join()
- print("process number: %s " % str(ctr+1).zfill(3) + str(test_d["data %d"%ctr]))
- stop = time.clock()
- print('CPUs*: %d time: %f' % (CPUs, stop-t))
- print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement