multiprocessing3_queue.py 596 B

12345678910111213141516171819202122232425
  1. # View more python learning tutorial on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. import multiprocessing as mp
  5. def job(q):
  6. res = 0
  7. for i in range(1000):
  8. res += i+i**2+i**3
  9. q.put(res) # queue
  10. if __name__ == '__main__':
  11. q = mp.Queue()
  12. p1 = mp.Process(target=job, args=(q,))
  13. p2 = mp.Process(target=job, args=(q,))
  14. p1.start()
  15. p2.start()
  16. p1.join()
  17. p2.join()
  18. res1 = q.get()
  19. res2 = q.get()
  20. print(res1+res2)