python---基礎知識回顧(十)進程和線程(自訂線程池,上下文管理器和協程的使用)

來源:互聯網
上載者:User

標籤:阻塞   基礎知識   設定   printf   elf   div   name   __name__   簡單   

前戲:在進行自訂線程池前,先瞭解下Queue隊列

隊列中可以存放基礎資料類型,也可以存放類,對象等特殊資料類型

from queue import Queueclass T:    def __init__(self,num):        self.num = num    def printf(self):        print(self.num,id(self.num))if __name__ == "__main__":    queue = Queue(0)    num = 12    queue.put(num)  #可以存放基礎資料類型    t = T(num)    queue.put(t)    #可以存放對象    cls = T    queue.put(cls)  #可以存放類    dt = queue.get()    print(id(dt),dt,type(dt))   #1385649280 12 <class ‘int‘>    dt = queue.get()    print(id(dt),dt,type(dt))   #7652128 <__main__.T object at 0x000000000074C320> <class ‘__main__.T‘>    dt = queue.get()    print(id(dt),dt,type(dt))   #18042264 <class ‘__main__.T‘> <class ‘type‘>

 

線程池應該具備的功能:
  1. 先建立線程池,
  2. 之後去擷取資料的時候,若是有直接拿走
  3. 若是沒有,需要去阻塞等待,直到有資料到達
  4. 線程池可以設定指定大小
  5. 滿足上面“阻塞”要求,設定大小,的資料結構,我們可以使用Queue隊列
簡單版本(Low):

 

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.