国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用Python的線程來解決生產者消費問題的示例

2020-02-23 00:31:19
字體:
來源:轉載
供稿:網友

我們將使用Python線程來解決Python中的生產者—消費者問題。這個問題完全不像他們在學校中說的那么難。

如果你對生產者—消費者問題有了解,看這篇博客會更有意義。

為什么要關心生產者—消費者問題:

    可以幫你更好地理解并發和不同概念的并發。     信息隊列中的實現中,一定程度上使用了生產者—消費者問題的概念,而你某些時候必然會用到消息隊列。

當我們在使用線程時,你可以學習以下的線程概念:

    Condition:線程中的條件。     wait():在條件實例中可用的wait()。     notify() :在條件實例中可用的notify()。

我假設你已經有這些基本概念:線程、競態條件,以及如何解決靜態條件(例如使用lock)。否則的話,你建議你去看我上一篇文章basics of Threads。

引用維基百科:

生產者的工作是產生一塊數據,放到buffer中,如此循環。與此同時,消費者在消耗這些數據(例如從buffer中把它們移除),每次一塊。

這里的關鍵詞是“同時”。所以生產者和消費者是并發運行的,我們需要對生產者和消費者做線程分離。
 

from threading import Thread class ProducerThread(Thread):  def run(self):    pass class ConsumerThread(Thread):  def run(self):    pass

再次引用維基百科:

這個為描述了兩個共享固定大小緩沖隊列的進程,即生產者和消費者。

假設我們有一個全局變量,可以被生產者和消費者線程修改。生產者產生數據并把它加入到隊列。消費者消耗這些數據(例如把它移出)。

queue = []

在剛開始,我們不會設置固定大小的條件,而在實際運行時加入(指下述例子)。

一開始帶bug的程序:

from threading import Thread, Lockimport timeimport random queue = []lock = Lock() class ProducerThread(Thread):  def run(self):    nums = range(5) #Will create the list [0, 1, 2, 3, 4]    global queue    while True:      num = random.choice(nums) #Selects a random number from list [0, 1, 2, 3, 4]      lock.acquire()      queue.append(num)      print "Produced", num      lock.release()      time.sleep(random.random()) class ConsumerThread(Thread):  def run(self):    global queue    while True:      lock.acquire()      if not queue:        print "Nothing in queue, but consumer will try to consume"      num = queue.pop(0)      print "Consumed", num      lock.release()      time.sleep(random.random()) ProducerThread().start()ConsumerThread().start()

運行幾次并留意一下結果。如果程序在IndexError異常后并沒有自動結束,用Ctrl+Z結束運行。

樣例輸出:
 

Produced 3Consumed 3Produced 4Consumed 4Produced 1Consumed 1Nothing in queue, but consumer will try to consumeException in thread Thread-2:Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner  self.run() File "producer_consumer.py", line 31, in run  num = queue.pop(0)IndexError: pop from empty list            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 繁昌县| 曲周县| 准格尔旗| 千阳县| 宝应县| 微博| 察隅县| 洪洞县| 贵阳市| 灵石县| 凤阳县| 柳州市| 犍为县| 河池市| 泰来县| 图片| 南召县| 武平县| 涟源市| 涞源县| 南丹县| 江口县| 通化县| 定南县| 疏附县| 临漳县| 陆川县| 施秉县| 谷城县| 禹城市| 信阳市| 临洮县| 乐清市| 镇平县| 烟台市| 南投县| 奉新县| 乐陵市| 常山县| 河曲县| 黑河市|