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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷心兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值