python 線程條件變數鎖

來源:互聯網
上載者:User

標籤:bsp   not   eve   init   condition   signal   join   odi   mail   

# _*_coding:utf-8_*_# author:leo# date:# email:[email protected]import queue, threadingclass Worker(threading.Thread):    def __init__(self):        threading.Thread.__init__(self)        self.lock = threading.Lock()        self.con1 = threading.Condition(self.lock)        self.con2 = threading.Condition(self.lock)        self.event = threading.Event()# 也可以達到類似的效果,但是不夠靈活,運用的情境不夠豐富,此類也是安全執行緒        self.queque = queue.Queue()        print(self.event.is_set())        def run(self):        threading.Thread(target=self.customer).start()        threading.Thread(target=self.producter).start()    def producter(self):        with self.con1:            for a in range(100):                print(‘productor put:‘, str(a))                self.queque.put(a)                self.con2.notify()                self.con1.wait() #使用wait 必須保證當前是安全的 必須得到鎖                print(‘recevie sigal from con1 producting...‘)            self.con2.notify_all()    def customer(self):        with self.con2:            while True:                self.con2.wait()                print(‘recevie signal from con2 customer....‘)                if self.queque.empty():                    break                result = self.queque.get()                if result is not None:                    print(‘customer get:‘, str(result))                    self.con1.notify()t = Worker()t.start()t.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.