利用Process类创建多个子进程对象执行任务,主进程负责调度

import time
from multiprocessing import Process

def run1():
    for i in range(5):
        print("sunck is a good man")
        time.sleep(1)

def run2(name, word):
    for i in range(8):
        print("%s is a %s man"%(name, word))
        time.sleep(1)


if __name__ == "__main__":
    t1 = time.time()

    # 后期主进程主要做的是调度相关的工作,不负责具体业务逻辑
    pro1 = Process(target=run1, args=())
    pro1.start()
    pro2 = Process(target=run2, args=("kaige", "cool"))
    pro2.start()

    pro1.join()
    pro2.join()




    t2 = time.time()
    print("耗时:%2f"%(t2-t1))
sunck is a good man
kaige is a cool man
sunck is a good man
kaige is a cool man
sunck is a good man
kaige is a cool man
sunck is a good man
kaige is a cool man
sunck is a good man
kaige is a cool man
kaige is a cool man
kaige is a cool man
kaige is a cool man
耗时:8.223099

猜你喜欢

转载自www.cnblogs.com/wuygblog/p/10758833.html