第一种:创建Process对象
import os,time from multiprocessing import Process def func(arg1,arg2): print("*"*arg1) # print("进程号:",os.getpid()) # print("父进程:",os.getppid()) time.sleep(0.01) print("="*arg2) if __name__ == '__main__': p_lst = [] for i in range(1,11): p = Process(target=func,args=(10*i,20*i)) p_lst.append(p) p.start() [p.join() for p in p_lst] print("game over")
第二种:创建自定义类并继承Process类,必须实现run方法,进程启动自动调用run方法
import os from multiprocessing import Process class MyProcess(Process): def __init__(self,arg1,arg2): super().__init__() self.arg1 = arg1 self.arg2 = arg2 def run(self): print(os.getpid()) if __name__ == '__main__': p1 = MyProcess(1,2) p1.start() p2 = MyProcess(3,4) p2.start() print("主进程:",os.getpid())