Guest User

Untitled

a guest
Jan 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # multi-processes
  2. from multiprocessing import Process, Queue
  3. class Worker(object):
  4. def __init__(self, queue):
  5. self.queue = queue
  6. self.process_num = 10 <------------ 10 processes
  7. self.count = 0
  8.  
  9. def start(self):
  10. for i in range(self.process_num):
  11. p = Process(target = self.run)
  12. p.start()
  13. p.join()
  14.  
  15. def run(self):
  16. while True:
  17. self.count += 1
  18. user = self.queue.get()
  19. # do something not so fast like time.sleep(1)
  20. print self.count
  21. if self.queue.empty():
  22. break
  23.  
  24. def start(self):
  25. for i in range(self.process_num):
  26. p = Process(target = self.run)
  27. p.start() <-- starts a new process
  28. p.join() <-- waits for the process to terminate
Add Comment
Please, Sign In to add comment