# 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
I use Worker().start(queue) to start the program, but the output is not so fast as i expected(Seems only one process are running).
Is there any problem in my code ?
Yes, you’re only running one process at a time, you’re waiting for each process to terminate before starting the next;
In other words, you’re starting 10 processes, but the second one won’t start until the first one terminates and so on.
For what you’re trying to do, it may be better not to use
Processmanually and instead use a Process Pool.