Python Asyncio模塊實現(xiàn)的生產(chǎn)消費者模型的方法
asyncio的關(guān)鍵字說明
event_loop事件循環(huán):程序開啟一個無限循環(huán),把一些函數(shù)注冊到事件循環(huán)上,當滿足事件發(fā)生的時候,調(diào)用相應的協(xié)程函數(shù) coroutine協(xié)程:協(xié)程對象,指一個使用async關(guān)鍵字定義的函數(shù),它的調(diào)用不會立即執(zhí)行函數(shù),而是會返回一個協(xié)程對象,協(xié)程對象需要注冊到事件循環(huán),由事件循環(huán)調(diào)用。 task任務(wù):一個協(xié)程對象就是一個原生可以掛起的函數(shù),任務(wù)則是對協(xié)程進一步封裝,其中包含了任務(wù)的各種狀態(tài) future:代表將來執(zhí)行或沒有執(zhí)行的任務(wù)結(jié)果。它和task上沒有本質(zhì)上的區(qū)別 async/await關(guān)鍵字:async定義一個協(xié)程,await用于掛起阻塞的異步調(diào)用接口,在python3.4是使用asyncio.coroutine/yield from在設(shè)計模式中,生產(chǎn)消費者模型占有非常重要的地位,這個模型在現(xiàn)實世界中也有很多有意思的對應場景,比如做包子的人和吃包子的人,當兩者速度不匹配時,就需要有一個模型來做匹配(偶合),實現(xiàn)做的包子都會依次消費掉。
import asyncioclass ConsumerProducerModel: def __init__(self, producer, consumer, queue=asyncio.Queue(), plate_size=6): # the plate holds 6pcs bread self.queue = queue self.producer = producer self.consumer = consumer self.plate_size = plate_size async def produce_bread(self): for i in range(self.plate_size): bread = f'bread {i}' await asyncio.sleep(0.5) # bread makes faster, 0.5s/pc await self.queue.put(bread) print(f’{self.producer} makes {bread}’) async def consume_bread(self): while True: bread = await self.queue.get() await asyncio.sleep(1) # eat slower, 1s/pc print(f’{self.consumer} eats {bread}’) self.queue.task_done()async def main(): queue = asyncio.Queue() cp1 = ConsumerProducerModel('John', 'Grace', queue) # group 1 cp2 = ConsumerProducerModel('Mike', 'Lucy', queue) # group 2 producer_1 = cp1.produce_bread() producer_2 = cp2.produce_bread() consumer_1 = asyncio.ensure_future(cp1.consume_bread()) consumer_2 = asyncio.ensure_future(cp2.consume_bread()) await asyncio.gather(*[producer_1, producer_2]) await queue.join() consumer_1.cancel() consumer_2.cancel()if __name__ == ’__main__’: loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close()
生產(chǎn)消費者模型可以使用多線程和隊列來實現(xiàn),這里選擇協(xié)程不僅是因為性能不錯,而且整個下來邏輯清晰:
1. 先定義初始化的東西,要有個隊列,要有生產(chǎn)者,要有消費者,要有裝面包的盤子大小;
2. 生產(chǎn)者:根據(jù)盤子大小生產(chǎn)出對應的東西(面包),將東西放入盤子(queue);
3. 消費者:從盤子上取東西,每次取東西都是一個任務(wù),每次任務(wù)完成,就標記為task_done(調(diào)用函數(shù))。在這個層面,一直循環(huán);
4. 主邏輯:實例化生產(chǎn)消費者模型對象,創(chuàng)建生產(chǎn)者協(xié)程,創(chuàng)建任務(wù)(ensure_future),收集協(xié)程結(jié)果,等待所有線程結(jié)束(join),手動取消兩個消費者協(xié)程;
5. 運行:首先創(chuàng)建事件循環(huán),然后進入主邏輯,直到完成,關(guān)閉循環(huán)。
到此這篇關(guān)于Python Asyncio模塊實現(xiàn)的生產(chǎn)消費者模型的方法的文章就介紹到這了,更多相關(guān)Python生產(chǎn)消費者模型內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)知識VBScript基本元素講解2. ajax請求添加自定義header參數(shù)代碼3. Kotlin + Flow 實現(xiàn)Android 應用初始化任務(wù)啟動庫4. Python requests庫參數(shù)提交的注意事項總結(jié)5. 基于javascript處理二進制圖片流過程詳解6. Gitlab CI-CD自動化部署SpringBoot項目的方法步驟7. 解決android studio引用遠程倉庫下載慢(JCenter下載慢)8. asp知識整理筆記4(問答模式)9. axios和ajax的區(qū)別點總結(jié)10. 詳談ajax返回數(shù)據(jù)成功 卻進入error的方法
