#encoding:utf-8
from multiprocessing import Process
import os, time, random
def r1(process_name):
for i in range(5):
print(f'{process_name} {os.getpid()}') #打印出当前进程的id
time.sleep(1)
def r2(process_name):
for i in range(5):
print(f'{process_name} {os.getpid()}') #打印出当前进程的id
time.sleep(1.2)
if __name__ == "__main__":
print("main process run...")
p1 = Process(target=r1, args=('process_name1', ))
p2 = Process(target=r2, args=('process_name2', ))
#所以使用多进程的常规方法是,先依次调用start启动进程,再依次调用join要求主进程等待子进程的结束。
p1.start()
p2.start()
p1.join() #join() : 阻塞当前进程,直到调用join方法的那个进程执行完,再继续执行当前进程。
#p2.join()
print("main process runned all lines...")
网友评论