python threading Condition 條件變量

原因:在生產者跟消費者模式中,消費者判斷有沒有產品,這就形成一個判斷。。。

import logging
import threading
import time
from random import randint
from threading import Condition

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [*] %(message)s"
)
L = []
threads = []
lock_con = Condition()


class Producer(threading.Thread):
    def run (self):
        global L
        while True:
            # 創建隨機數
            val = randint(0, 100)
            if lock_con.acquire():
                L.append(val)
                logging.info(f"{self.name}生產->{str(val)} 容器->{L}")
                lock_con.notifyAll()  # 通知一個正在wait方法通過
                lock_con.release()
            time.sleep(0.5)


class Consumer(threading.Thread):
    def __init__ (self):
        super().__init__()
        self.name = "迷心兔"
    
    def run (self):
        global L
        while True:
            time.sleep(0.5)
            if len(L) == 0:
                # 等到收到通知或發生超時爲止。
                # 必須在已獲得鎖前提下才能調用,否則會觸發RuntimeError
                lock_con.wait()
            
            if lock_con.acquire():
                logging.info(f"{self.name}消費->{str(L[0])} 容器->{L}")
                del L[0]
                lock_con.release()


if __name__ == '__main__':
    # 五個生產者-----------------------------
    for i in range(2):
        threads.append(Producer())
    
    # 一個消費者-----------------------------
    threads.append(Consumer())
    
    # 阻塞啓動線程---------------------------
    for t in threads:
        t.start()
    for t in threads:
        t.join()

輸出:

2019-10-02 16:48:01,087 [*] Thread-1生產->80 容器->[80]
2019-10-02 16:48:01,088 [*] Thread-2生產->40 容器->[80, 40]
2019-10-02 16:48:01,588 [*] Thread-1生產->61 容器->[80, 40, 61]
2019-10-02 16:48:01,589 [*] 迷心兔消費->80 容器->[80, 40, 61]
2019-10-02 16:48:01,589 [*] Thread-2生產->43 容器->[40, 61, 43]
2019-10-02 16:48:02,089 [*] Thread-1生產->50 容器->[40, 61, 43, 50]
2019-10-02 16:48:02,090 [*] Thread-2生產->72 容器->[40, 61, 43, 50, 72]
2019-10-02 16:48:02,091 [*] 迷心兔消費->40 容器->[40, 61, 43, 50, 72]
2019-10-02 16:48:02,591 [*] Thread-1生產->19 容器->[61, 43, 50, 72, 19]
2019-10-02 16:48:02,592 [*] Thread-2生產->45 容器->[61, 43, 50, 72, 19, 45]
2019-10-02 16:48:02,593 [*] 迷心兔消費->61 容器->[61, 43, 50, 72, 19, 45]
2019-10-02 16:48:03,092 [*] Thread-1生產->63 容器->[43, 50, 72, 19, 45, 63]
2019-10-02 16:48:03,093 [*] Thread-2生產->32 容器->[43, 50, 72, 19, 45, 63, 32]
2019-10-02 16:48:03,095 [*] 迷心兔消費->43 容器->[43, 50, 72, 19, 45, 63, 32]
2019-10-02 16:48:03,594 [*] Thread-1生產->50 容器->[50, 72, 19, 45, 63, 32, 50]
2019-10-02 16:48:03,595 [*] Thread-2生產->18 容器->[50, 72, 19, 45, 63, 32, 50, 18]
2019-10-02 16:48:03,597 [*] 迷心兔消費->50 容器->[50, 72, 19, 45, 63, 32, 50, 18]
2019-10-02 16:48:04,095 [*] Thread-1生產->70 容器->[72, 19, 45, 63, 32, 50, 18, 70]
2019-10-02 16:48:04,096 [*] Thread-2生產->44 容器->[72, 19, 45, 63, 32, 50, 18, 70, 44]
2019-10-02 16:48:04,098 [*] 迷心兔消費->72 容器->[72, 19, 45, 63, 32, 50, 18, 70, 44]
2019-10-02 16:48:04,597 [*] Thread-1生產->89 容器->[19, 45, 63, 32, 50, 18, 70, 44, 89]
2019-10-02 16:48:04,598 [*] Thread-2生產->32 容器->[19, 45, 63, 32, 50, 18, 70, 44, 89, 32]
2019-10-02 16:48:04,600 [*] 迷心兔消費->19 容器->[19, 45, 63, 32, 50, 18, 70, 44, 89, 32]
2019-10-02 16:48:05,098 [*] Thread-1生產->5 容器->[45, 63, 32, 50, 18, 70, 44, 89, 32, 5]
2019-10-02 16:48:05,099 [*] Thread-2生產->10 容器->[45, 63, 32, 50, 18, 70, 44, 89, 32, 5, 10]
2019-10-02 16:48:05,101 [*] 迷心兔消費->45 容器->[45, 63, 32, 50, 18, 70, 44, 89, 32, 5, 10]
2019-10-02 16:48:05,598 [*] Thread-1生產->13 容器->[63, 32, 50, 18, 70, 44, 89, 32, 5, 10, 13]
2019-10-02 16:48:05,599 [*] Thread-2生產->30 容器->[63, 32, 50, 18, 70, 44, 89, 32, 5, 10, 13, 30]
2019-10-02 16:48:05,602 [*] 迷心兔消費->63 容器->[63, 32, 50, 18, 70, 44, 89, 32, 5, 10, 13, 30]
2019-10-02 16:48:06,099 [*] Thread-1生產->19 容器->[32, 50, 18, 70, 44, 89, 32, 5, 10, 13, 30, 19]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章