网络编程:自定义进程类

创建自定义进程类

  【1】 编写类继承Process

  【2】 在自定义类中加载父类__init__以获取父类属性,同时可以自定义新的属性 

  【3】重写run方法,在调用start时自动执行该方法

from multiprocessing import Process 
import time 

class ClockProcess(Process):
    def __init__(self,value):
        #调用父类init
        super().__init__()
        self.value = value 
    #重写run方法
    def run(self):
        for i in range(5):
            time.sleep(self.value)
            print("The time is {}".format(time.ctime()))

p = ClockProcess(2)
#自动执行run
p.start()

p.join()

 

posted @ 2021-01-07 22:46  昱成  阅读(63)  评论(0编辑  收藏  举报