欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python生成器和基于生成器的協(xié)程

 更新時間:2021年06月03日 14:50:48   作者:WinvenChang  
說到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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論