日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯Python教程:python線程通信Condition提供的方法

1、acquire調用condition關聯的方法。

我們提供的服務有:成都網站設計、成都做網站、微信公眾號開發(fā)、網站優(yōu)化、網站認證、三河ssl等。為近1000家企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的三河網站制作公司

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)
 
 
# 創(chuàng)建一個賬戶
account = Account("賬戶一", 0)
# 創(chuàng)建并啟動取錢線程
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()
# 創(chuàng)建并啟動存錢線程
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()

以上就是python線程通信Condition提供的方法,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯python教程

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。


網站題目:創(chuàng)新互聯Python教程:python線程通信Condition提供的方法
URL地址:http://www.5511xx.com/article/cojdjeo.html