Advertisement
here2share

# generic_multiprocessing.py

Jul 9th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # generic_multiprocessing.py
  2.  
  3. from multiprocessing.pool import ThreadPool
  4. import multiprocessing
  5. import time
  6.  
  7. p=10
  8.  
  9. def worker(x):
  10.     time.sleep(1) # to similate delayed processing of variable p seconds
  11.     s = "[%s: time = %s]" % (x+1, ms())
  12.     print s; return s # note: to print while multiprocessing may appear conficting in itself
  13. #
  14. def start(): return time.time()
  15. def ms(): return int((time.time()-st)*100)/100.00
  16.  
  17. print "test without multiprocessing --" # waaaaait for it...
  18. st = start()
  19. for i in range(p):
  20.     worker(i)
  21. print
  22. print ms(),
  23.    
  24. print "seconds\n\n\ntest with the multiprocessing --"
  25. def group(i):
  26.     w = worker(i)
  27.     return w
  28. st = start()
  29. # CPUs = multiprocessing.cpu_count() ###??? far slower than ThreadPool(128)
  30. pool = ThreadPool(128)
  31. results = []
  32. results.append(pool.map(group, range(p)))
  33. print results[0] # automatically sorted? haha... yeahhhhh!
  34. print
  35. print ms(), 'seconds'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement