当前位置: 首页 » 综合知识 » it知识 » 正文

python线程通信Condition提供的方法有哪些

发布时间:2023-07-24 以下文章来源于网友投稿,内容仅供参考!

1、acquire调用Condition关联的方法。

Lock的acquire()或release()。

2、wait传入timeout参数。

指定该线程最多等待多少秒。

导致当前线程进入Condition的等待池等待通知并释放锁,直到其他线程调用该Condition的notify()或者notify_all()方法来唤醒该线程。在调用该wait()方法时可以

3、notify唤醒Condition的单个线程并通知。

收到通知的线程会自动调用accquire()方法尝试加锁。如果所有线程都在该Condition等待池中等待,则会选择唤醒其中一个线程,选择是任意性的。

4、notify_all唤醒所有线程并通知。

实例

import threading
class Account:
    # 定义构造函数
    def __init__(self, account_no, balance):
        self.account_no = account_no
        self._balance = balance
        self.condition = threading.Condition()
        # 定义代表是否已经存钱的标识
        self.__deposit_flag = False
 
    # 取钱
    def draw(self, draw_amount):
        # 加锁
        self.condition.acquire()
        try:
            # 还没存钱
            if not self.__deposit_flag:
                self.condition.wait()
            else:
                if self._balance >= draw_amount:
                    self._balance = self._balance - draw_amount
                    print(threading.current_thread().getName() + " 取钱成功,账户余额是:" + str(self._balance) + "\n")
                else:
                    print(threading.current_thread().getName() + " 取钱失败\n")
                # 将标识账户已有存款的标识改成False
                self.__deposit_flag = False
                # 唤醒其他等待现车线程
                self.condition.notify_all()
        finally:
            # 释放锁
            self.condition.release()
 
    # 存钱
    def deposit(self, deposit_amount):
        # 加锁
        self.condition.acquire()
        try:
            # 如果已经存款了,则等待取款
            if self.__deposit_flag:
                self.condition.wait()
            else:
                self._balance = self._balance + deposit_amount
                print(threading.current_thread().getName() + " 存款成功,存款金额是:" + str(deposit_amount) + "\n")
                # 将存款标识改成已存款
                self.__deposit_flag = True
                # 唤醒其他线程
                self.condition.notify_all()
        finally:
            # 释放锁
            self.condition.release()
 
 
def draw_many(account, draw_amount, max):
    for i in range(max):
        account.draw(draw_amount)
 
 
def deposit_many(account, deposit_amount, max):
    for i in range(max):
        account.deposit(deposit_amount)
 
 
# 创建一个账户
account = Account("账户一", 0)
# 创建并启动取钱线程
draw_1 = threading.Thread(name='取钱者一', target=draw_many, args=(account, 200, 50))
draw_1.start()
draw_2 = threading.Thread(name='取钱者二', target=draw_many, args=(account, 200, 50))
draw_2.start()
# 创建并启动存钱线程
deposit_1 = threading.Thread(name='存钱者一', target=deposit_many, args=(account, 200, 50))
deposit_1.start()
deposit_2 = threading.Thread(name='存钱者二', target=deposit_many, args=(account, 200, 50))
deposit_2.start()
draw_1.join()
draw_2.join()
deposit_1.join()
deposit_2.join()
  • • Linux Ecdsa密钥长度选择有何依据

    在Linux

  • • Linux Khook在内核监控中的应用如何

    Linux

  • • Linux Gsoap是否支持异步通信

    GSOAP是

  • • Linux Coremail如何提升用户体验

    提升Linu

  • • Linux Ecdsa算法有哪些局限性

    ECDSA

  • 哎呀音乐钢琴键盘学习《 钢琴主人训练营》 西瓜学琴
    郭蝈 陪练钢琴 30节课时 考级刚需 让孩子每一次练琴都是高质量的
    30天轻松学会五线谱 流行钢琴自学初级教程 牙牙学琴
    流行爵士钢琴实战技巧VIP课 - 继伟 哎呀音乐
    【海上钢琴师】原版 MT1990钢琴谱
    百首经典流行钢琴实战曲集 - 继伟
    雷费尔德电钢琴重锤88键专业考级儿童初学者数码电子钢琴家用
    小练咖 真人钢琴陪练 1v1服务 2999随时退 1课时50分钟 考级刚需
    雅马哈电钢琴88键重锤CLP735智能数码电子钢琴家用专业初学者考级
    【原装进口】Yamaha/雅马哈钢琴 b121 SC2原声静音钢琴
  • 珠海专业调钢琴
  • 天津宝坻区调钢琴
  • 天津静海区钢琴调音
  • 成都简阳市钢琴调律
  • 大连瓦房店市钢琴调音
  • 眉山调钢琴联系方式
  • 惠州大亚湾钢琴调琴师
  • 长治调琴师
  • 厦门湖里区钢琴调音师
  • 上海普陀区钢琴调音师