Python使用asyncio包處理并發(fā)的實現(xiàn)代碼
使用 asyncio 包處理并發(fā)
asyncio包:使用事件循環(huán)驅(qū)動的協(xié)程實現(xiàn)并發(fā)。
線程與協(xié)程的對比
'\ thinking' 旋轉(zhuǎn)等待效果
In [1]: import threading
In [2]: import itertools
In [3]: import time,sys
In [4]: class Signal: # 定義一個簡單的可變對象;go 屬性 從外部控制線程
...: go = True
In [5]: def spin(msg,signal):
...: w,flush = sys.stdout.write,sys.stdout.flush
...: for char in itertools.cycle('|/-\\'): # 從序列中反復(fù)不斷的生成元素
...: status = char + ' ' + msg
...: w(status)
...: flush()
...: w('\x08' * len(status)) # 退格鍵:\x08 文本動畫的訣竅所在
...: time.sleep(1)
...: if not signal.go:
...: break
...: w(' ' * len(status) + '\x08' * len(status))
In [6]: def slow():
...: time.sleep(3)
...: return 42
In [9]: def super():
...: signal = Signal()
...: sp = threading.Thread(target=spin,args=('thinking',signal))
...: print('============')
...: sp.start()
...: res = slow()
...: signal.go = False
...: sp.join()
...: return res
注意:Python 沒有提供終止線程的 API ,這是有意為之的。若想關(guān)閉線程,必須給線程發(fā)送消息。這里用的是 signal.go 屬性。干凈的規(guī)則的退出。
適合 asyncio API 的協(xié)程:
1 定義體必須使用 yield from ,而不能使用 yield
2 協(xié)程要由調(diào)用方驅(qū)動,并由調(diào)用方通過 yield from 調(diào)用
3 或者把協(xié)程傳給 asyncio 包中的某個函數(shù),比如 asyncio.async()
4 @asyncio.coroutine 裝飾器應(yīng)該用在協(xié)程上
asyncio 實現(xiàn) 動畫效果
In [1]: import asyncio
In [3]: import itertools
In [4]: import sys
# 交給 asyncio 處理的協(xié)程需要使用該裝飾器裝飾。這不是強制要求,但是強烈建議這么做。
In [5]: @asyncio.coroutine
...: def spin(msg): # 不需要多線程的關(guān)閉參數(shù)
...: w,flush = sys.stdout.write, sys.stdout.flush
...: for char in itertools.cycle('|/-\\'):
...: status = char + ' ' + msg
...: w(status)
...: flush()
...: w('\x08' * len(status))
...: try:
...: yield from asyncio.sleep(.1) # 不會阻塞事件循環(huán)
# spin 函數(shù)蘇醒后,取消請求 異常,退出循環(huán)
...: except asyncio.CancelledError:
...: break
...: write(' ' * len(status) + '\x08' * len(status))
...:
In [6]: @asyncio.coroutine
...: def slow():
# 把控制權(quán)交給主循環(huán),休眠結(jié)束后,結(jié)束這個協(xié)程
...: yield from asyncio.sleep(3)
...: return 42
...:
In [9]: @asyncio.coroutine
...: def sup():
# asyncio 排定spin協(xié)程的運行時間,封裝成一個 Task對象 sp
...: sp = asyncio.async(spin('thinking!'))
...: print('spin obj:',sp)
# sup 也是協(xié)程,因此,可以使用 yield from 驅(qū)動 slow()
...: res = yield from slow()
sp.cancel()
...: return res
...:
In [10]: def main():
...: loop = asyncio.get_event_loop()
...: res = loop.run_until_complete(sup())
...: loop.close()
...: print('answer:',res)
...:
In [11]: main()
D:\python36\Scripts\ipython:3: DeprecationWarning: asyncio.async() function is deprecated, use ensure_future()
spin obj: <Task pending coro=<spin() running at <ipython-input-5-0304845f34e1>:1>>
answer: 42!
除非想阻塞主線程,從而凍結(jié)事件循環(huán)或整個應(yīng)用,否則不要在 asyncio 協(xié)程中使用 time.sleep() 。如果協(xié)程需要一段時間內(nèi)什么也不做,應(yīng)該使用 yield from asyncio.sleep() 。
@asyncio.coroutine 裝飾器不是強制要求,但是強烈建議這么做,因為這樣能
1 把協(xié)程凸顯出來,有助于調(diào)試。
2 如果還未產(chǎn)出值,協(xié)程就被垃圾回收了(意味著有操作未完成,因此有可能是個缺陷),那就可以發(fā)出警告了。
3 這個裝飾器不會預(yù)激協(xié)程。
到此這篇關(guān)于Python使用asyncio包處理并發(fā)的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Python asyncio包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python數(shù)據(jù)抓取分析的示例代碼(python + mongodb)
本篇文章主要介紹了python數(shù)據(jù)抓取分析的示例代碼(python + mongodb),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
微信小程序前端如何調(diào)用python后端的模型詳解
近期需要開發(fā)一個打分的微信小程序,涉及到與后臺服務(wù)器的數(shù)據(jù)交互,這篇文章主要給大家介紹了關(guān)于微信小程序前端如何調(diào)用python后端模型的相關(guān)資料,需要的朋友可以參考下2022-04-04
Python實現(xiàn)銀行賬戶資金交易管理系統(tǒng)
這篇文章主要介紹了Python銀行賬戶資金交易管理系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
nx.adjacency_matrix計算鄰接矩陣與真實結(jié)果不一致的解決
這篇文章主要介紹了nx.adjacency_matrix計算鄰接矩陣與真實結(jié)果不一致的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
利用Python實現(xiàn)簡單的Excel統(tǒng)計函數(shù)
這篇文章主要介紹了利用Python實現(xiàn)簡單的Excel統(tǒng)計函數(shù),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07

