在进程中:join方法 是让主进程等待子进程运行完毕后再执行主进程的。(即主进程阻塞)
示例
# -*- coding: utf-8 -*- from multiprocessing import Process import time class MyProcess(Process): def __init__(self, name, n): super().__init__() self.name = name self.n = n def run(self): print('进程%s is running,%s' % (self.n, self.name)) time.sleep(self.n) print('进程%s is done %s' % (self.n, self.name)) if __name__ == '__main__': p_ls = [] start_time = time.time() for i in range(1,5): p = MyProcess('qzk', i) p_ls.append(p) p.start() for p in p_ls: p.join() print(' 主进程执行', time.time() - start_time) # 进程1 is running,qzk # 进程4 is running,qzk # 进程2 is running,qzk # 进程3 is running,qzk # 进程1 is done qzk # 进程2 is done qzk # 进程3 is done qzk # 进程4 is done qzk # 主进程执行 4.15123724937439