继承Process类,计算累加和以及阶乘
#定义一个类 继承Process类 from multiprocessing import Process import os class Download(Process): def __init__(self,num): Process.__init__(self) self.num = num # print(type(self.num))<class 'str'> def run(self): print('子进程(%s)开始执行,父进程为(%s)'%(os.getpid(),os.getppid())) #计算阶乘 def func(n): if n == 1: return 1 return n*func(n-1) t = func(int(self.num)) print(t) #计算累加和 def fun(n): if n == 1: return 1 return n + fun(n-1) ob = fun(int(self.num)) print(ob) if __name__ == "__main__": print("当前程序主进程id: (%s)"%os.getpid()) num = input('Enter number:') p = Download(num) #这里调用了run p.start()