开启进程的方式

方式一,使用默认类:

from multiprocessing import Process
import time
def task(name):
    print("%s is running" % name)
    time.sleep(3)
    print("%s is done" % name)
if __name__=="__main__":
    #Process(target=task,kwargs={"name":"子进程1"})
    p=Process(target=task,args=("子进程1",))#按位置参数传参
    p.start()#给操作系统发送指令,开启子进程
    print("主进程")

方式二,自定义类:

from multiprocessing import Process
import time
class MyProcess(Process):
    def __init__(self,name):
        super(MyProcess,self).__init__()
        self.name=name
    def run(self):
        print("%s is running" % self.name)
        time.sleep(5)
        print("%s is done" % self.name)

if __name__ == "__main__":
    p=MyProcess("子进程")
    p.start()

猜你喜欢

转载自www.cnblogs.com/yaya625202/p/9029478.html