Python 多進程多線程原理介紹以及原創的Python多進程和多線程模板 ...
一、Python 多進程多線程原理介紹
1. Python 全局解釋器鎖GIL
a) Python的全局解釋器鎖GIL是互斥鎖,能夠防止本機多個線程一次執行Python位元組碼;由於CPython的記憶體管理線上程級別是不安全的(記憶體泄露),所以這個全局解釋器鎖是必須的。每個Python進程只能申請使用一個GIL鎖,因此Python的多線程雖然是併發的但不能並行處理。Python的解釋器每次只能執行一個線程,待GIL鎖釋放後再執行下一個線程,這樣線程輪流被執行。
b) Python2.x里,GIL的釋放邏輯是當前線程遇見IO操作或者ticks計數達到100;python3.x中,GIL不使用ticks計數,改為使用計時器即執行時間達到設定閾值後,釋放當前線程的GIL。
2. Python 多進程多線程適用場景
a) CPU-bound (計算密集型)
CPU-bound 指的是系統的硬碟/記憶體效能相對CPU的效能要好很多,系統運行時的狀態是CPU占用接近100%;I/O讀寫硬碟和記憶體占用很低。在這種計算密集型的狀況下Python的ticks計數器很快達到閾值會觸發GIL的釋放與再鎖定。這時Python多線程會頻繁的加鎖和釋放鎖,消耗大量的CPU資源。因此對於計算密集型的程式Python使用多進程要比多線程好很多。
b) I/O-bound (I/O密集型)
I/O-bound 指的是系統的CPU效能相對硬碟/記憶體的效能要好很多,系統運行時大部分時間是在等待I/O讀寫,CPU的占用並不高。Python單線程下有I/O操作會進行I/O等待,造成不必要的CPU時間浪費,開啟多線程能線上程A執行I/O等待時,自動切換到線程B,可以不浪費CPU的資源,從而能提升程式執行效率。因此對於I/O密集型的程式Python多線程有一定的優勢。
二、 Python 多進程多線程編程模板
1. 此模板具有兩個重要優點:
a) 根據程式執行類型自定義多進程多線程的開啟個數,調節程式的執行效率。
b) 程式中還實時列印程式執行進度並不較大的影響程式的執行效率。
1 #!/usr/bin/env python 2 # -*- coding: UTF-8 -*- 3 __author__="阿輝楓情" 4 __date__ = "$2017-5-12 21:49:51$" 5 import os 6 import sys 7 import time 8 import random 9 import threading 10 from multiprocessing import Process, Manager, Lock 11 12 # 解決編碼問題 13 reload(sys) 14 sys.setdefaultencoding('utf-8') 15 Type = sys.getfilesystemencoding() 16 17 #------------------------------------------------ 18 # 可修改的全局變數參數--Start. 19 tasklist = [] # 定義任務列表 20 21 PROCESS_COUNT = 2 # 定義進程數量 22 THREAD_COUNT = 4 # 定義線程數量 23 # 可修改全局變數參數--End. 24 #------------------------------------------------ 25 26 27 class HandleTask(threading.Thread): 28 """docstring for HandleTask""" 29 30 def __init__(self, proid, prolock, thrid, thrlock, tasklist, tasknum, schedule): 31 super(HandleTask, self).__init__() 32 self.proid = proid 33 self.prolock = prolock 34 self.thrid = thrid 35 self.thrlock = thrlock 36 self.tasklist = tasklist 37 self.tasknum = tasknum 38 self.sch = schedule 39 self.pid = os.getpid() 40 41 def run(self): 42 self.prolock.acquire() 43 self.thrlock.acquire() 44 print "The Thread [%s:%s] tasklist number:[%s]" % (self.proid, self.thrid, len(self.tasklist)) 45 self.thrlock.release() 46 self.prolock.release() 47 48 for (element, ) in self.tasklist: 49 # 任務執行開始 50 # print element 51 time.sleep(1) 52 # 任務執行結束 53 54 self.prolock.acquire() 55 self.thrlock.acquire() 56 self.sch.value += 1 57 self.thrlock.release() 58 self.prolock.release() 59 60 def Thread_Handle(proid, prolock, tasklist, tasknum, schedule): 61 global THREAD_COUNT 62 lock = threading.Lock() 63 WorksThread = [] 64 thread_task_number = len(tasklist) / THREAD_COUNT 65 if thread_task_number == 0: 66 THREAD_COUNT = len(tasklist) 67 thread_task_number = 1 68 69 for i in range(THREAD_COUNT): 70 if i != THREAD_COUNT - 1: 71 source_list = tasklist[i * thread_task_number: (i + 1) * thread_task_number] 72 else: 73 source_list = tasklist[i * thread_task_number:] 74 Work = HandleTask(proid, prolock, i, lock, source_list, tasknum, schedule) 75 Work.start() 76 WorksThread.append(Work) 77 78 for Work in WorksThread: 79 Work.join() 80 81 def Process_Handle(tasklist, tasknum): 82 global PROCESS_COUNT 83 lock = Lock() 84 # 定義進度變數 schedule 85 schedule = Manager().Value('schedule', 0) 86 WorksProcess = [] 87 # 按照任務大小進行進程任務分配 88 process_task_num = len(tasklist) / PROCESS_COUNT 89 if process_task_num == 0: 90 PROCESS_COUNT = len(tasklist) 91 process_task_num = 1 92 93 for i in range(PROCESS_COUNT): 94 if i != PROCESS_COUNT - 1: 95 source_list = tasklist[i * process_task_num: (i + 1) * process_task_num] 96 else: 97 source_list = tasklist[i * process_task_num:] 98 Work = Process(target=Thread_Handle, args=(i, lock, source_list, tasknum, schedule)) 99 Work.start() 100 WorksProcess.append(Work) 101 # 添加額外進程列印任務執行進度 102 Work = Process(target=Displays, args=(lock, tasknum, schedule)) 103 Work.start() 104 WorksProcess.append(Work) 105 for Work in WorksProcess: 106 Work.join() 107 del WorksProcess 108 109 def Displays(prolock, tasknum, schedule, delaytime=None): 110 if delaytime is None: 111 delaytime = 1 112 while (tasknum - schedule.value): 113 time.sleep(delaytime) 114 print "Completed:[%s] , Remaining:[%s]" % (schedule.value, tasknum - schedule.value) 115 116 def main(): 117 # 列印輸出主進程號 118 print "The Main Process ID:[%s]"% os.getpid() 119 # 建立測試任務 120 for i in range(1, 101): 121 tasklist.append((i, )) 122 Process_Handle(tasklist, len(tasklist)) 123 124 125 if __name__ == '__main__': 126 127 print "The Program start time:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 128 start = time.time() 129 main() 130 print "The Program end time:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "[%s]" % (time.time() - start) 131 raw_input("Please enter any key to end!!!".decode('utf-8').encode(Type))View Code
2. 程式執行測試結果
1 The Program start time: 2017-05-12 18:15:12 2 The Main Process ID:[9752] 3 The Thread [0:0] tasklist number:[12] 4 The Thread [1:0] tasklist number:[12] 5 The Thread [1:1] tasklist number:[12] 6 The Thread [0:1] tasklist number:[12] 7 The Thread [1:2] tasklist number:[12] 8 The Thread [0:2] tasklist number:[12] 9 The Thread [0:3] tasklist number:[14] 10 The Thread [1:3] tasklist number:[14] 11 Completed:[0] , Remaining:[100] 12 Completed:[8] , Remaining:[92] 13 Completed:[16] , Remaining:[84] 14 Completed:[25] , Remaining:[75] 15 Completed:[34] , Remaining:[66] 16 Completed:[42] , Remaining:[57] 17 Completed:[51] , Remaining:[49] 18 Completed:[59] , Remaining:[41] 19 Completed:[67] , Remaining:[32] 20 Completed:[76] , Remaining:[24] 21 Completed:[85] , Remaining:[15] 22 Completed:[93] , Remaining:[7] 23 Completed:[98] , Remaining:[2] 24 Completed:[100] , Remaining:[0] 25 The Program end time: 2017-05-12 18:15:27 [15.007999897] 26 Please enter any key to end!!!View Code