1. 程式人生 > >《可愛的Python》讀書筆記(九)

《可愛的Python》讀書筆記(九)

多線程 threading

KISS 才是王道!


KISS == Keep It Simple,Stupid

不論什麽,堅持簡單的過程/原理/結構/代碼,就是自在!

現在小白想使用多線程來提高查詢的速度,就用threading模塊了!

# -*- coding: utf-8 -*-
import os
import time
from threading import Thread
from configparser import RawConfigParser as rcp


class grepIt(Thread):

    def __init__(self, cdcfile, keyword):
    
        Thread.__init__(self)
        self.cdcf = cdcfile
        self.keyw = keyword.upper()
        self.report = ""
    def run(self):
    
        if self.cdcf.endswith('.ini'):
            self.report = marklni(self.cdcf, self.keyw)
            
            
def marklni(cdcfile, keyword):
    """配置文件模式匹配函數
    """
    report = ""
    keyw = keyword.upper()
    cfg = rcp()
    cfg.read(cdcfile)
    nodelist = cfg.sections()
    nodelist.remove("Comment")
    nodelist.remove("Info")
    for node in nodelist:
        if keyw in node.upper():
            print(node)
            report += "\n %s" % node
            continue
        else:
            for item in cfg.items(node):
                if keyw in item[0].upper():
                    report += "\n %s\%s" % (node, item)
    return report
    
def grepSearch(cdcpath, keyword):
    """多線程群體搜索函數
    """
    begin = time.time()
    filelist = os.listdir(cdcpath)
    # 用於記錄發起的搜索線程
    searchlist = []
    for cdcf in filelist:
        pathcdcf = "%s\%s" % (cdcpath, cdcf)
        #print(pathcdcf)
        # 初始化線程對象
        current = grepIt(pathcdcf, keyword)
        # 追加記錄線程隊列
        searchlist.append(current)
        # 發動線程處理
        current.start()
    for searcher in searchlist:
        searcher.join()
        print("Search from", searcher.cdcf, "out", searcher.report)
    print("usage %s s" % (time.time() - begin))
    
    
if __name__ == "__main__":

    grepSearch("F:\\back", "images")

針對執行效率的重構,就像玩頭腦急轉彎,只要充分直覺地分析運行時的整體軟件行為,很容易確定瓶頸。

對問題有了準確的理解後,再向行者請教時便有了確切方向,進而容易獲得有效的提示。


小練習:

利用Lock和RLock實現線程間的簡單同步,使得10個線程對同一共享變量進行遞增操作,使用加鎖機制保證變量結果的正確

# -*- coding: utf-8 -*-
import threading, time


class mythread(threading.Thread):

    def __init__(self):
    
        threading.Thread.__init__(self)
        
    def run(self):
    
        global n
        if lock.acquire():
            print('Thread:',n)
            n += 1
            lock.release()
            
n = 0
t = []
lock = threading.Lock()
for i in range(10):
    my = mythread()
    t.append(my)
for i in range(10):
    t[i].start()
for i in range(10):
    t[i].join()

運行結果如下:

Thread: 0
Thread: 1
Thread: 2
Thread: 3
Thread: 4
Thread: 5
Thread: 6
Thread: 7
Thread: 8
Thread: 9


《可愛的Python》讀書筆記(九)