重構父類方法和返回值

今天用面向對象寫了一段代碼,用建立的類重構父類的方法,實現函數鎖能實現的功能:代碼以下app

 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())
重構父類方法

這段代碼的執行結果是:ide

 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 spa

發現有返回值.code

而本身在這裏的想法是直接用super方法調用父類方法後,會執行後返回一個結果,可是這裏的使用super()方法後,會是本身重構的方法具備父類的屬性,須要一個變量去接,若是須要的話能夠返回這個值對象

若是沒有返回值的話,就直接調用父類的方法blog

修改後的代碼以下:進程

 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())
修改的代碼
相關文章
相關標籤/搜索