Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # multi-processes
- from multiprocessing import Process, Queue
- class Worker(object):
- def __init__(self, queue):
- self.queue = queue
- self.process_num = 10 <------------ 10 processes
- self.count = 0
- def start(self):
- for i in range(self.process_num):
- p = Process(target = self.run)
- p.start()
- p.join()
- def run(self):
- while True:
- self.count += 1
- user = self.queue.get()
- # do something not so fast like time.sleep(1)
- print self.count
- if self.queue.empty():
- break
- def start(self):
- for i in range(self.process_num):
- p = Process(target = self.run)
- p.start() <-- starts a new process
- p.join() <-- waits for the process to terminate
Add Comment
Please, Sign In to add comment