目錄
- 一、什么是生成器
- 二、基于生成器的協程
- 三、協程的注意點
- 四、協程裝飾器
- 五、python3原生協程
一、什么是生成器
Generator
1.生成器就是可以生成值的函數
2.當一個函數里有了 yield
關鍵字就成了生成器
3.生成器可以掛起執行并且保持當前執行的狀態
代碼示例:
def simple_gen():
yield 'hello'
yield 'world'
gen = simple_gen()
print(type(gen)) # 'generator' object
print(next(gen)) # 'hello'
print(next(gen)) # 'world'
二、基于生成器的協程
Python3
之前沒有原生協程,只有基于生成器的協程
1.pep
342(Coroutines via Enhanced Generators
)增強生成器功能
2.生成器可能通過 yield
暫停執行和產出數據
3.同時支持send()
向生成器發送數據和throw()
向生成器拋出異常
Generator Based Corouteine代碼示例:
def coro():
hello = yield 'hello' # yield 關鍵字在 = 右邊作為表達式,可以被 send 值
yield hello
c = coro()
# 輸出 'hello', 這里調用 next 產出第一個值 'hello',之后函數暫停
print(next(c))
# 再次調用 send 發送值,此時 hello 變量賦值為 'world',然后 yield 產出 hello 變量的值 'world'
print(c.send('world'))
# 之后協程結束,后續再 send 值會拋出異常 StopIteration
運行結果:

三、協程的注意點
協程注意點
1.協程需要使用send(None)
或者next(coroutine)
來預激(prime
)才能啟動
2.在yield
處協程會暫停執行
3.單獨的yield value
會產出值給調用方
4.可以通過 coroutine.send(value)
來給協程發送值,發送的值會賦值給 yield
表達式左邊的變量value = yield
5.協程執行完成后(沒有遇到下一個yield
語句)會拋出StopIteration
異常
四、協程裝飾器
避免每次都要用 send
預激它
from functools import wraps
def coroutine(func): # 這樣就不用每次都用 send(None) 啟動了
“”“裝飾器:向前執行到一個 `yield` 表達式,預激 `func` ”“”
@wrops(func)
def primer(*args, **kwargs): # 1
gen = func(*args, **kwargs) # 2
next(gen) # 3
return gen # 4
return primer
五、python3原生協程
python3.5引入 async/await支持原生協程(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()
到此這篇關于詳解Python生成器和基于生成器的協程的文章就介紹到這了,更多相關Python生成器與協程內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 深入理解python協程
- python在協程中增加任務實例操作
- python 如何引入協程和原理分析
- python3爬蟲中異步協程的用法
- Python協程的方式實現及意義筆記分享