多进程并行
code
import time from multiprocessing import Process def f(name): print('hello', name) time.sleep(1) if __name__ == '__main__': p_lst = [] for i in range(5): p = Process(target=f, args=('bob',)) p.start() p_lst.append(p) #p.join() x=[p.join() for p in p_lst] print('父进程在执行')
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py
hello bob
hello bob
hello bob
hello bob
hello bob
父进程在执行
macname@MacdeMacBook-Pro py %
多进程串行
code
import time from multiprocessing import Process def f(name): print('hello', name) time.sleep(1) if __name__ == '__main__': p_lst = [] for i in range(5): p = Process(target=f, args=('bob',)) p.start() p_lst.append(p) p.join() #x=[p.join() for p in p_lst] print('父进程在执行')
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py
hello bob
hello bob
hello bob
hello bob
hello bob
父进程在执行
macname@MacdeMacBook-Pro py %