詳解Python生成器和基于生成器的協(xié)程
一、什么是生成器
Generator
1.生成器就是可以生成值的函數(shù)
2.當一個函數(shù)里有了 yield
關(guān)鍵字就成了生成器
3.生成器可以掛起執(zhí)行并且保持當前執(zhí)行的狀態(tài)
代碼示例:
def simple_gen(): yield 'hello' yield 'world' gen = simple_gen() print(type(gen)) # 'generator' object print(next(gen)) # 'hello' print(next(gen)) # 'world'
二、基于生成器的協(xié)程
Python3
之前沒有原生協(xié)程,只有基于生成器的協(xié)程
1.pep
342(Coroutines via Enhanced Generators
)增強生成器功能
2.生成器可能通過 yield
暫停執(zhí)行和產(chǎn)出數(shù)據(jù)
3.同時支持send()
向生成器發(fā)送數(shù)據(jù)和throw()
向生成器拋出異常
Generator Based Corouteine代碼示例:
def coro(): hello = yield 'hello' # yield 關(guān)鍵字在 = 右邊作為表達式,可以被 send 值 yield hello c = coro() # 輸出 'hello', 這里調(diào)用 next 產(chǎn)出第一個值 'hello',之后函數(shù)暫停 print(next(c)) # 再次調(diào)用 send 發(fā)送值,此時 hello 變量賦值為 'world',然后 yield 產(chǎn)出 hello 變量的值 'world' print(c.send('world')) # 之后協(xié)程結(jié)束,后續(xù)再 send 值會拋出異常 StopIteration
運行結(jié)果:
三、協(xié)程的注意點
協(xié)程注意點
1.協(xié)程需要使用send(None)
或者next(coroutine)
來預(yù)激(prime
)才能啟動
2.在yield
處協(xié)程會暫停執(zhí)行
3.單獨的yield value
會產(chǎn)出值給調(diào)用方
4.可以通過 coroutine.send(value)
來給協(xié)程發(fā)送值,發(fā)送的值會賦值給 yield
表達式左邊的變量value = yield
5.協(xié)程執(zhí)行完成后(沒有遇到下一個yield
語句)會拋出StopIteration
異常
四、協(xié)程裝飾器
避免每次都要用 send
預(yù)激它
from functools import wraps def coroutine(func): # 這樣就不用每次都用 send(None) 啟動了 “”“裝飾器:向前執(zhí)行到一個 `yield` 表達式,預(yù)激 `func` ”“” @wrops(func) def primer(*args, **kwargs):# 1 gen = func(*args, **kwargs) # 2 next(gen) # 3 return gen # 4 return primer
五、python3原生協(xié)程
python3.5引入 async/await支持原生協(xié)程(native coroutine)
import asyncio import datetime import random async def display_date(num, loop): end_time = loop.time() + 50.0 while True: print('Loop: {} Time: {}'.format(num, datetime.datetime.now()) if (loop.time() + 1.0) >= end_time: break await asyncio.sleep(random.randint(0, 5)) loop = asyncio.get_event_loop() asyncio.ensure_future(display_date(1, loop)) asyncio.ensure_future(display_date(2, loop)) loop.run_forever()
到此這篇關(guān)于詳解Python生成器和基于生成器的協(xié)程的文章就介紹到這了,更多相關(guān)Python生成器與協(xié)程內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。