使用Process类创建进程:
import multiprocessing
import time
def test():
n = 5
while n>0:
print(n)
time.sleep(1)
n -= 1
if __name__ == '__main__':
p = multiprocessing.Process(target=test)#Process类中的run方法执行
p.start()
p.join()
创建MyProcess类,继承Process:
class MyProcess(multiprocessing.Process):
def run(self):
n = 5
while n > 0:
print(n)
time.sleep(1)
n -= 1
if __name__ == '__main__':
p = MyProcess()#不需要指明target,因为在子类中已经重写了run方法,它的target已经固定在子类中run方法
p.start()
p.join()
p = MyProcess()不需要指明target,因为在子类中已经重写了run方法,它的target已经固定在子类中run方法