python-多線程等概念

來源:互聯網
上載者:User

標籤:AC   pytho   資料   code   執行   clear   map   rgs   play   

並發 & 並行

並發:是指系統具有處理多個任務的能力

並行:是指系統具有 同時 處理多個任務的能力

並行 是  並發的一個子集

 

同步 & 非同步

同步:當進程執行到一個I/O(等待外部資料的時候)----------等 : 同步

非同步:                     ----------不等,直到接收到資料再回來執行

 

 

GIL:全域解釋鎖

  因為有GIL鎖,所以同一時刻,只有一個線程被CPU執行

 

任務:IO密集型

   計算密集型

 

對於IO密集型:Python 的多線程是有意義的

       可以採用多進程+協程

對於計算密集型:Python 的多線程就不推薦了,不適用了。

 

 

RLock(遞迴鎖)

說白了就是在一個大鎖中還要再包含子鎖

import threading,time def run1():    print("grab the first part data")    lock.acquire()    global num    num +=1    lock.release()    return numdef run2():    print("grab the second part data")    lock.acquire()    global  num2    num2+=1    lock.release()    return num2def run3():    lock.acquire()    res = run1()    print(‘--------between run1 and run2-----‘)    res2 = run2()    lock.release()    print(res,res2)  if __name__ == ‘__main__‘:     num,num2 = 0,0    lock = threading.RLock()    for i in range(10):        t = threading.Thread(target=run3)        t.start() while threading.active_count() != 1:    print(threading.active_count())else:    print(‘----all threads done---‘)    print(num,num2)
View Code

 

Semaphore(訊號量)

互斥鎖 同時只允許一個線程更改資料,而Semaphore是同時允許一定數量的線程更改資料 ,比如廁所有3個坑,那最多隻允許3個人上廁所,後面的人只能等裡面有人出來了才能再進去。

import threading,time def run(n):    semaphore.acquire()    time.sleep(1)    print("run the thread: %s\n" %n)    semaphore.release() if __name__ == ‘__main__‘:     num= 0    semaphore  = threading.BoundedSemaphore(5) #最多允許5個線程同時運行    for i in range(20):        t = threading.Thread(target=run,args=(i,))        t.start() while threading.active_count() != 1:    pass #print threading.active_count()else:    print(‘----all threads done---‘)    print(num)
View Code

Event

通過Event來實現兩個或多個線程間的互動,下面是一個紅綠燈的例子,即起動一個線程做交通指揮燈,產生幾個線程做車輛,車輛行駛按紅燈停,綠燈行的規則。

import threading,timeimport randomdef light():    if not event.isSet():        event.set() #wait就不阻塞 #綠燈狀態    count = 0    while True:        if count < 10:            print(‘\033[42;1m--green light on---\033[0m‘)        elif count <13:            print(‘\033[43;1m--yellow light on---\033[0m‘)        elif count <20:            if event.isSet():                event.clear()            print(‘\033[41;1m--red light on---\033[0m‘)        else:            count = 0            event.set() #開啟綠燈        time.sleep(1)        count +=1def car(n):    while 1:        time.sleep(random.randrange(10))        if  event.isSet(): #綠燈            print("car [%s] is running.." % n)        else:            print("car [%s] is waiting for the red light.." %n)if __name__ == ‘__main__‘:    event = threading.Event()    Light = threading.Thread(target=light)    Light.start()    for i in range(3):        t = threading.Thread(target=car,args=(i,))        t.start()
View Code

隊列queue  (重點)

1,queue.Queue()  FIFO隊列-先進先出

2,queue.LifoQueue() LIFO隊列,先進後出

3,queue.PriorityQueue()  按照優先順序,越低越先出

方法:

q.size 返回隊列大小

q.empty() 如果隊列為空白返回true

q.full() 如果隊列為滿返回true

q.get () 裡面可以設定block 的t,f

q.put()

q.join()實際是隊列為空白時再執行別的操作

 

python-多線程等概念

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.