亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python的條件鎖與事件共享

系統 1829 0

1:事件機制共享隊列:

利用消息機制在兩個隊列中,通過傳遞消息,實現可以控制的生產者消費者問題
要求:readthread讀時,writethread不能寫;writethread寫時,readthread不能讀。
基本方法 時間類(Event)
·set:設置事件。將標志位設為True。
wait:等待事件。會將當前線程阻塞,直到標志位變為True。
clear:清除事件。將標志位設為False。
set() clear() 函數的交替執行 也就是消息傳遞的本質

          

模版:
            
              基本code
# 事件消息機制
import queue
import threading
import random

            
            
              from
            
            
               threading import Event

            
            
              from
            
            
               threading import Thread

            
            
              class
            
            
               WriteThread(Thread):
    def __init__(self,q,wt,rt):
        super().__init__();
        self.queue
            
            =
            
              q;
        self.rt
            
            =
            
              rt;
        self.wt
            
            =
            
              wt;
   def  run(self):
         self.rt.
            
            
              set
            
            
              ()
         
         self.wt.wait();
         self.wt.clear();
         

            
            
              class
            
            
               ReadThread(Thread):
    def __init__(self,q,wt,rt):
        super().__init__();
        self.queue
            
            =
            
              q;
        self.rt
            
            =
            
              rt;
        self.wt
            
            =
            
              wt;    
     def run(self):
          
            
            
              while
            
            
               True:
             self.rt.wait();
             self.wt.wait();
             self.wt.clear()
            
          

?

參考代碼:

            
              #
            
            
               -*- coding: utf-8 -*-
            
            
              """
            
            
              
Created on Tue Sep 10 20:10:10 2019

@author: DGW-PC

            
            
              """
            
            
              #
            
            
               事件消息機制
            
            
              import
            
            
               queue

            
            
              import
            
            
               threading

            
            
              import
            
            
               random

            
            
              from
            
             threading 
            
              import
            
            
               Event

            
            
              from
            
             threading 
            
              import
            
            
               Thread


            
            
              class
            
            
               WriteThread(Thread):
    
            
            
              def
            
            
              __init__
            
            
              (self,q,wt,rt):
        super().
            
            
              __init__
            
            
              ();
        self.queue
            
            =
            
              q;
        self.rt
            
            =
            
              rt;
        self.wt
            
            =
            
              wt;
    
            
            
              def
            
            
               run(self):
        data
            
            =[random.randint(1,100) 
            
              for
            
             _ 
            
              in
            
             range(0,10
            
              )];
        self.queue.put(data);
        
            
            
              print
            
            (
            
              "
            
            
              WriteThread寫隊列:
            
            
              "
            
            
              ,data);
        self.rt.set(); 
            
            
              #
            
            
               發送讀事件
            
            
              print
            
            (
            
              "
            
            
              WriteThread通知讀
            
            
              "
            
            
              );
        
            
            
              print
            
            (
            
              "
            
            
              WriteThread等待寫
            
            
              "
            
            
              );
        self.wt.wait();
        
            
            
              print
            
            (
            
              "
            
            
              WriteThread收到寫事件
            
            
              "
            
            
              );
        self.wt.clear();
        
            
            6

            
              class
            
            
               ReadThread(Thread):
    
            
            
              def
            
            
              __init__
            
            
              (self,q,wt,rt):
        super().
            
            
              __init__
            
            
              ();
        self.queue
            
            =
            
              q;
        self.rt
            
            =
            
              rt;
        self.wt
            
            =
            
              wt;
    
            
            
              def
            
            
               run(self):
        
            
            
              while
            
            
               True:
            self.rt.wait();
            
            
              #
            
            
               等待寫事件 帶來
            
            
              print
            
            (
            
              "
            
            
              ReadThread 收到讀事件
            
            
              "
            
            
              );
            
            
            
              print
            
            (
            
              "
            
            
              ReadThread 開始讀{0}
            
            
              "
            
            
              .format(self.queue.get()));
            
            
            
              print
            
            (
            
              "
            
            
              ReadThread 發送寫事件
            
            
              "
            
            
              );
            self.wt.set();
            self.rt.clear();
q
            
            =
            
              queue.Queue();
rt
            
            =
            
              Event();
wt
            
            =
            
              Event();
writethread
            
            =WriteThread(q,wt,rt); 
            
              #
            
            
               實例化對象的
            
            
readthread=ReadThread(q,wt,rt);   
            
              #
            
            
               實例化對象的
            
            
              
writethread.start();
readthread.start();
            
          

?



2:條件鎖同步生產者消費者

作用: 在保護互斥資源的基礎上,增加了條件判斷的機制
即為使用wait() 函數 判斷不滿足當前條件的基礎上,讓當前線程的阻塞。
其他線程如果生成了滿足了條件的資源 使用notify() notifyALl() 函數將刮起線程喚醒。
使用了 threading 的Condition 類
acquire() : 鎖住當前資源
relarse() :釋放當前鎖住的資源
wait:掛起當前線程, 等待喚起 。
? notify:喚起被 wait 函數掛起的線程 。
? notif計All:喚起所有線程,防止線程永遠處于沉默狀態 。

?
模版:

            
              基本code

            
            
              from
            
            
               threading import Thread

            
            
              from
            
            
               threading import Condition
import random
import time

            
            
              lock
            
            =
            
              Condition(); # 聲明條件鎖
flag
            
            =
            
              0
            
            
              ;
def cnsumer():
    
            
            
              lock
            
            
              .acquire();
    
            
            
              while
            
             flag==
            
              0
            
            
              :
        
            
            
              lock
            
            
              .wait();
   
   業務代碼
            
            ---        

            
              lock
            
            
              .relarse();
      
def product():
    
            
            
              lock
            
            
              .acquire();
    
    釋放鎖之前對控制變量進行操作,數據的操作控制 可以作為全局變量來鎖定
    
            
            
              lock
            
            
              .notifyALl();
    
            
            
              lock
            
            .relarse();
            



?

?

?參考代碼code:

            
              #
            
            
               -*- coding: utf-8 -*-
            
            
              """
            
            
              
Created on Wed Sep 11 21:40:41 2019

@author: DGW-PC

            
            
              """
            
            
              #
            
            
               條件鎖生產者消費者
            
            
              from
            
             threading 
            
              import
            
            
               Thread

            
            
              from
            
             threading 
            
              import
            
            
               Condition

            
            
              import
            
            
               random

            
            
              import
            
            
               time

flag
            
            =0; 
            
              #
            
            
               聲明控制標志
            
            
goods=0; 
            
              #
            
            
               事物表示
            
            
lock=
            
              Condition();

            
            
              def
            
            
               consumer(x):
    
            
            
              global
            
            
               flag;
    
            
            
              global
            
            
               goods;
    lock.acquire(); 
            
            
              #
            
            
               取得鎖
            
            
              while
            
             flag==0: 
            
              #
            
            
               便于多次進行消費
            
            
              print
            
            (
            
              "
            
            
              consumer %d進入等待
            
            
              "
            
             %
            
               x);
         lock.wait();
    
            
            
              print
            
            (
            
              "
            
            
              consumer {0}:消費了{1}
            
            
              "
            
            .format(x,goods));
            
              #
            
            
               format 次序從0開始
            
            
    flag-=1
            
              ;
    lock.release(); 
            
            
              #
            
            
              釋放鎖
            
            
              def
            
            
               product(x):
    
            
            
              global
            
            
               flag;
    
            
            
              global
            
            
               goods;
    time.sleep(
            
            3
            
              );
    lock.acquire();
    goods
            
            =random.randint(1,1000
            
              );
    
            
            
              print
            
            (
            
              "
            
            
              product {0} 產生了{1}
            
            
              "
            
            
              .format(x,goods));
    flag
            
            +=1
            
              ;
    lock.notifyAll();
    lock.release();

threads
            
            =
            
              [];


            
            
              for
            
             i 
            
              in
            
             range(0,2
            
              ):
    t1
            
            =Thread(target=consumer,args=
            
              (i,));
    t2
            
            =Thread(target=product,args=
            
              (i,));
    t1.start();
    t2.start();
    threads.append(t1);
    threads.append(t2);


            
            
              for
            
             x 
            
              in
            
            
               threads:
    x.join();
    
            
          

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品国产乱子伦多人 | 欧美精品久久久久久久久大尺度 | 乱人伦99久久 | 亚洲精品美女久久久aaa | 久久精品国产亚洲片 | 大片国产片日本观看免费视频 | 国内精品日本久久久久影院 | 一区二区三区免费视频网站 | 色婷婷在线观看视频 | 老师邪恶影院a啦啦啦影院 老师在办公室被躁到白浆 老湿机午夜影院 | 亚洲国产小视频 | 日韩欧国产精品一区综合无码 | 全部无卡免费的毛片在线看 | 国产成人久久精品激情 | 色综合久久亚洲国产日韩 | 天天干夜夜操 | 青草娱乐极品免费视频 | 久久天天躁狠狠躁夜夜不卡 | xxxx日本在线播放免费不卡 | 日韩中文视频 | 久久不卡一区二区三区 | 美美女高清毛片视频黄的一免费 | 日日碰狠狠添天天爽爽爽 | 女人一级毛片 | 四虎影视免费观看免费观看 | 国产中文字幕在线免费观看 | 亚洲视频一二三 | 国产成年网站 | 欧美大片国产在线永久播放 | 免费黄色一级大片 | 一级毛片全部免费播放 | 免费h片在线观看 | 3级毛片| 精品国产一区二区三区久久 | 国产chinesehd在线观看 | 毛片黄片一级片 | 欧美精品啪啪 | 日本免费一区二区三区a区 日本免费一区二区三区看片 | 久草成人 | 久久伊人精品 | 九九视频免费精品视频免费 |