Advertisement
kpfp_linux

Python 2 multiprocessing

Jan 5th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. from multiprocessing import Process, Queue
  2.  
  3. def do_sum(q,l):
  4.     res = 0
  5.     for i in range(10000):
  6.         res += sum(l)
  7.     q.put(res)
  8.  
  9. def main():
  10.     my_list = range(1000000)
  11.     cores = 8
  12.  
  13.     n = len(my_list) / cores
  14.     chunks = (my_list[i:i+n] for i in xrange(0, len(my_list), n))
  15.  
  16.     q = Queue()
  17.     ps = [Process(target=do_sum, args=(q, chunk))
  18.           for chunk in chunks]
  19.     print len(ps)
  20.     for p in ps:
  21.         p.start()
  22.     print sum(q.get() for p in ps)
  23.  
  24.  
  25. if __name__=='__main__':
  26.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement