from multiprocessing import Process, Queue
class A:
def F1(self, q, code1, code2):
data = -1
q.put(data)
def F2(self, q, code1):
q2 = Queue()
for i in range(10):
'''
some processing here
'''
p = Process(target=self.F1, args=(q2, i, j))
p.start()
print(q2.get())
p.join()
def Handler(self):
q = Queue()
for i in range(10):
p = Process(target=self.F2, args=(q, i))
p.start()
print(q.get())
p.join()
if __name__ == "__main__":
app = A()
app.Handler()
执行后我发现代码以线性方式执行,而不是使用多处理。我无法弄清楚原因?
答案 0 :(得分:1)
问题是通过在q2.get
循环中调用for
(例如),您等待每个进程完成后再启动另一个进程。您的调度员可以按如下方式进行更改,以便让所有10名同时在后台工作。
def F2(self, q, code1):
q2 = Queue()
processes = []
# start all of the processes
for i in range(10):
'''
some processing here
'''
p = Process(target=self.F1, args=(q2, i, j))
p.start()
processes.append(p)
# get data for all processes. buggy because an exception in the
# child is not caught and will cause program to hang
for i in range(10):
print(q2.get())
#dispose of the processes
for p in processes:
p.join()
multiprocessing
已经有Pool
类为你工作并处理启动异常。