调用父类方法和返回值

今天用面向对象写了一段代码,用创建的类重构父类的方法,实现函数锁能实现的功能:代码如下

 1 from os import getpid
 2 from time import sleep
 3 from multiprocessing import Process
 4 
 5 
 6 class MyProcess(Process):
 7 
 8     def __init__(self, args):
 9         super(MyProcess, self).__init__()
10         self.args = args
11 
12     def run(self):
13         print('Process ID is %s' % getpid())
14         print('Process %s has been executed ' % self.args)
15 
16     def start(self):
17         print('Process %s will be executed ' % self.args)
18         super().start()
19 
20     def terminate(self):
21         super().terminate()
22 
23     def join(self, timeout=None):
24         super().join()
25 
26     def is_alive(self):
27         super().is_alive()
28 
29 
30 if __name__ == '__main__':
31     process_list = []
32     for i in range(10):
33         p = MyProcess(i)
34         p.start()
35         print(p.is_alive())
36         process_list.append(p)
37     for p in process_list:
38         print(p.is_alive())
39         print('There will terminated process %s' % p)
40         p.terminate()
41         sleep(0.2)      # 这里不延时0.2而直接打印的话,下边的is_alive会显示True
42         print(p.is_alive())
43         p.join()
44         print(p.is_alive())
重构父类方法

这段代码的执行结果是:

 1 Process 0 will be executed 
 2 None
 3 Process 1 will be executed 
 4 None
 5 Process 2 will be executed 
 6 None
 7 Process 3 will be executed 
 8 None
 9 Process 4 will be executed 
10 None
11 Process 5 will be executed 
12 None
13 Process 6 will be executed 
14 None
15 Process 7 will be executed 
16 None
17 Process 8 will be executed 
18 None
19 Process 9 will be executed 
20 None
21 None
22 There will terminated process <MyProcess(MyProcess-1, started)>
23 None
24 None
25 None
26 There will terminated process <MyProcess(MyProcess-2, started)>
27 Process ID is 14232
28 Process 3 has been executed 
29 None
30 None
31 None
32 There will terminated process <MyProcess(MyProcess-3, started)>
33 Process ID is 13276
34 Process 5 has been executed 
35 Process ID is 11380
36 Process 4 has been executed 
37 None
38 None
39 None
40 There will terminated process <MyProcess(MyProcess-4, stopped)>
41 Process ID is 13616
42 Process 6 has been executed 
43 Process ID is 13880
44 Process 7 has been executed 
45 Process ID is 9628
46 Process 8 has been executed 
47 Process ID is 13052
48 Process 9 has been executed 
49 None
50 None
51 None
52 There will terminated process <MyProcess(MyProcess-5, stopped)>
53 None
54 None
55 None
56 There will terminated process <MyProcess(MyProcess-6, stopped)>
57 None
58 None
59 None
60 There will terminated process <MyProcess(MyProcess-7, stopped)>
61 None
62 None
63 None
64 There will terminated process <MyProcess(MyProcess-8, stopped)>
65 None
66 None
67 None
68 There will terminated process <MyProcess(MyProcess-9, stopped)>
69 None
70 None
71 None
72 There will terminated process <MyProcess(MyProcess-10, stopped)>
73 None
74 None
执行结果

发现判断一个进进程是否还在运行后发现打印的全是None,看Process的源码:

 1 def is_alive(self): return False 

发现有返回值.

而自己在这里的想法是直接用super方法调用父类方法后,会执行后返回一个结果,但是这里的使用super()方法后,会是自己重构的方法具有父类的属性,需要一个变量去接,如果需要的话可以返回这个值

如果没有返回值的话,就直接调用父类的方法

修改后的代码如下:

 1 from os import getpid
 2 from time import sleep
 3 from multiprocessing import Process
 4 
 5 
 6 class MyProcess(Process):
 7 
 8     def __init__(self, args):
 9         super(MyProcess, self).__init__()
10         self.args = args
11 
12     def run(self):
13         print('Process ID is %s' % getpid())
14         print('Process %s has been executed ' % self.args)
15 
16     def start(self):
17         print('Process %s will be executed ' % self.args)
18         super().start()
19 
20     def terminate(self):
21         super().terminate()
22 
23     def join(self, timeout=None):
24         super().join()
25 
26     def is_alive(self):
27         try:
28             result = super().is_alive()
29             return result
30         except:
31             print("Something was wrong!")
32 
33 
34 if __name__ == '__main__':
35     process_list = []
36     for i in range(10):
37         p = MyProcess(i)
38         p.start()
39         print(p.is_alive())
40         process_list.append(p)
41     for p in process_list:
42         print(p.is_alive())
43         print('There will terminated process %s' % p)
44         p.terminate()
45         sleep(0.2)      # 这里不延时0.2而直接打印的话,下边的is_alive会显示True
46         print(p.is_alive())
47         p.join()
48         print(p.is_alive())
修改的代码

猜你喜欢

转载自www.cnblogs.com/linga/p/9392287.html