Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # multiprocessing_demo.py
- from multiprocessing import Pool, cpu_count
- import os, time
- def func(x):
- print x # output overlap as to be expected
- return (time.time()-start,x)
- start = time.time() # log start time
- if __name__ == "__main__": # <<< require for windows
- num_process = max(cpu_count(),2) # get the CPU count
- print("CPU Count: "+str(num_process))
- p = Pool(num_process) # create number of worker process based on the CPU count
- result=p.map(func,range(100)) # put the result in an iterator
- p.close() # close the worker process so it can be garbage collected
- p.join()
- result.sort()
- print
- print('-- output by time processed --')
- for t,p in result:
- print('%d\t%f'%(p,t))
- end = time.time()
- elapsed = end - start
- print(elapsed)
- # os.system("pause") # Pause at the end if console is used
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement