Python多进程multiprocessing的使用
创建程序
import multiprocessing as mp
import time
def test1(a,b):
print('test1',a)
time.sleep(5)
print('test1 sleep finished',b)
def test2(a,b):
print('test2',a)
time.sleep(10)
print('test2 sleep finished',b)
if __name__=='__main__':
p1 = mp.Process(target=test1,args=(1,2))
p2 = mp.Process(target=test2, args=(3,4))
p1.start()
p2.start()
p1.join()
p2.join()
print("main 5 continue")
运行结果
test1 1
test2 3
test1 sleep finished 2
test2 sleep finished 4
main 5 continue
Process finished with exit code 0