Advertisement
here2share

# multiprocessing_demo.py

Jul 31st, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. # multiprocessing_demo.py
  2.  
  3. from multiprocessing import Pool, cpu_count
  4. import os, time
  5.  
  6.  
  7. def func(x):
  8.     print x # output overlap as to be expected
  9.     return (time.time()-start,x)
  10.    
  11. start = time.time() # log start time
  12. if __name__ == "__main__": # <<< require for windows
  13.     num_process = max(cpu_count(),2) # get the CPU count
  14.     print("CPU Count: "+str(num_process))
  15.     p = Pool(num_process) # create number of worker process based on the CPU count
  16.     result=p.map(func,range(100)) # put the result in an iterator
  17.     p.close() # close the worker process so it can be garbage collected
  18.     p.join()
  19.     result.sort()
  20.     print
  21.     print('-- output by time processed --')
  22.     for t,p in result:
  23.         print('%d\t%f'%(p,t))
  24.     end = time.time()
  25.     elapsed = end - start
  26.     print(elapsed)
  27. #   os.system("pause") # Pause at the end if console is used
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement