日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python的條件鎖與事件共享

系統(tǒng) 2051 0

1:事件機(jī)制共享隊(duì)列:

利用消息機(jī)制在兩個(gè)隊(duì)列中,通過(guò)傳遞消息,實(shí)現(xiàn)可以控制的生產(chǎn)者消費(fèi)者問(wèn)題
要求:readthread讀時(shí),writethread不能寫(xiě);writethread寫(xiě)時(shí),readthread不能讀。
基本方法 時(shí)間類(Event)
·set:設(shè)置事件。將標(biāo)志位設(shè)為T(mén)rue。
wait:等待事件。會(huì)將當(dāng)前線程阻塞,直到標(biāo)志位變?yōu)門(mén)rue。
clear:清除事件。將標(biāo)志位設(shè)為False。
set() clear() 函數(shù)的交替執(zhí)行 也就是消息傳遞的本質(zhì)

          

模版:
            
              基本code
# 事件消息機(jī)制
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

            
            
              """
            
            
              #
            
            
               事件消息機(jī)制
            
            
              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寫(xiě)隊(duì)列:
            
            
              "
            
            
              ,data);
        self.rt.set(); 
            
            
              #
            
            
               發(fā)送讀事件
            
            
              print
            
            (
            
              "
            
            
              WriteThread通知讀
            
            
              "
            
            
              );
        
            
            
              print
            
            (
            
              "
            
            
              WriteThread等待寫(xiě)
            
            
              "
            
            
              );
        self.wt.wait();
        
            
            
              print
            
            (
            
              "
            
            
              WriteThread收到寫(xiě)事件
            
            
              "
            
            
              );
        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();
            
            
              #
            
            
               等待寫(xiě)事件 帶來(lái)
            
            
              print
            
            (
            
              "
            
            
              ReadThread 收到讀事件
            
            
              "
            
            
              );
            
            
            
              print
            
            (
            
              "
            
            
              ReadThread 開(kāi)始讀{0}
            
            
              "
            
            
              .format(self.queue.get()));
            
            
            
              print
            
            (
            
              "
            
            
              ReadThread 發(fā)送寫(xiě)事件
            
            
              "
            
            
              );
            self.wt.set();
            self.rt.clear();
q
            
            =
            
              queue.Queue();
rt
            
            =
            
              Event();
wt
            
            =
            
              Event();
writethread
            
            =WriteThread(q,wt,rt); 
            
              #
            
            
               實(shí)例化對(duì)象的
            
            
readthread=ReadThread(q,wt,rt);   
            
              #
            
            
               實(shí)例化對(duì)象的
            
            
              
writethread.start();
readthread.start();
            
          

?



2:條件鎖同步生產(chǎn)者消費(fèi)者

作用: 在保護(hù)互斥資源的基礎(chǔ)上,增加了條件判斷的機(jī)制
即為使用wait() 函數(shù) 判斷不滿足當(dāng)前條件的基礎(chǔ)上,讓當(dāng)前線程的阻塞。
其他線程如果生成了滿足了條件的資源 使用notify() notifyALl() 函數(shù)將刮起線程喚醒。
使用了 threading 的Condition 類
acquire() : 鎖住當(dāng)前資源
relarse() :釋放當(dāng)前鎖住的資源
wait:掛起當(dāng)前線程, 等待喚起 。
? notify:?jiǎn)酒鸨?wait 函數(shù)掛起的線程 。
? notif計(jì)All:?jiǎn)酒鹚芯€程,防止線程永遠(yuǎn)處于沉默狀態(tài) 。

?
模版:

            
              基本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();
   
   業(yè)務(wù)代碼
            
            ---        

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



?

?

?參考代碼code:

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

@author: DGW-PC

            
            
              """
            
            
              #
            
            
               條件鎖生產(chǎn)者消費(fèi)者
            
            
              from
            
             threading 
            
              import
            
            
               Thread

            
            
              from
            
             threading 
            
              import
            
            
               Condition

            
            
              import
            
            
               random

            
            
              import
            
            
               time

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

            
            
              def
            
            
               consumer(x):
    
            
            
              global
            
            
               flag;
    
            
            
              global
            
            
               goods;
    lock.acquire(); 
            
            
              #
            
            
               取得鎖
            
            
              while
            
             flag==0: 
            
              #
            
            
               便于多次進(jìn)行消費(fèi)
            
            
              print
            
            (
            
              "
            
            
              consumer %d進(jìn)入等待
            
            
              "
            
             %
            
               x);
         lock.wait();
    
            
            
              print
            
            (
            
              "
            
            
              consumer {0}:消費(fèi)了{(lán)1}
            
            
              "
            
            .format(x,goods));
            
              #
            
            
               format 次序從0開(kāi)始
            
            
    flag-=1
            
              ;
    lock.release(); 
            
            
              #
            
            
              釋放鎖
            
            
              def
            
            
               product(x):
    
            
            
              global
            
            
               flag;
    
            
            
              global
            
            
               goods;
    time.sleep(
            
            3
            
              );
    lock.acquire();
    goods
            
            =random.randint(1,1000
            
              );
    
            
            
              print
            
            (
            
              "
            
            
              product {0} 產(chǎn)生了{(lán)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();
    
            
          

?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。。?/p>

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 利辛县| 龙州县| 原平市| 廊坊市| 淅川县| 东台市| 同心县| 靖江市| 岳西县| 石泉县| 拉孜县| 宝应县| 山阳县| 多伦县| 时尚| 同德县| 六枝特区| 平阳县| 恭城| 和林格尔县| 射阳县| 乌兰浩特市| 台州市| 莎车县| 二连浩特市| 菏泽市| 浙江省| 卓资县| 兴隆县| 长治县| 孝义市| 于田县| 永登县| 中方县| 庆云县| 阿拉尔市| 旬邑县| 渑池县| 巴楚县| 句容市| 垫江县|