網(wǎng)站首頁 編程語言 正文
一、yield關(guān)鍵字實(shí)現(xiàn)方式
以yield關(guān)鍵字方式實(shí)現(xiàn)協(xié)程代碼如下所示:
def fun1():
yield 1
yield from fun2()
yield 2
def fun2():
yield 3
yield 4
f1=fun1()
for item in f1:
print(item)
在上述代碼中,一個(gè)Python函數(shù)中存在由yiled,就說明為生成器函數(shù),yield類似于普通函數(shù)的return,區(qū)別在于yield返回后Python會(huì)“記住”返回的位置,在下次返回時(shí)就從這個(gè)位置處返回。yiled from關(guān)鍵字后面跟著也是一個(gè)生成器,表示從該生成器返回。
在上述代碼運(yùn)行后,for循環(huán)就是一個(gè)迭代的過程,隨著迭代,每次fun1()生成器就會(huì)執(zhí)行一次“yield”。因此,上述函數(shù)會(huì)輸出1——3——4——2。該端代碼執(zhí)行結(jié)果如下所示:
上述代碼,其實(shí)并沒有完全的實(shí)現(xiàn)協(xié)程,或者說實(shí)現(xiàn)的協(xié)程非常牽強(qiáng)。
二、greenlet實(shí)現(xiàn)方式
greenlet是一個(gè)Python的第三方模塊,使用時(shí)需要提前安裝。執(zhí)行命令:
pip install greenlet
即可完成安裝。
greenlet實(shí)現(xiàn)協(xié)程代碼如下所示:
from greenlet import greenlet
def fun1():
print(1)
gr2.switch()
print(2)
gr2.switch()
def fun2():
print(3)
gr1.switch()
print(4)
gr1=greenlet(fun1)
gr2=greenlet(fun2)
gr1.switch()
在上述代碼中,greenlet()函數(shù)生成了一個(gè)greenlet對(duì)象,在該對(duì)象中調(diào)用switch()函數(shù)即可切換到指定greenlet對(duì)象對(duì)應(yīng)的函數(shù)中執(zhí)行,因此可以實(shí)現(xiàn)協(xié)程操作。
上述代碼執(zhí)行結(jié)果如下所示:
三、asyncio實(shí)現(xiàn)方式
在Python3.4以后,asyncio是Python的一個(gè)內(nèi)置模塊,因此無需安裝即可調(diào)用。使用asyncio模式實(shí)現(xiàn)的Python代碼如下所示:
import asyncio
@asyncio.coroutine
def fun1():
print(1)
yield from asyncio.sleep(2)
print(2)
@asyncio.coroutine
def fun2():
print(3)
yield from asyncio.sleep(2)
print(4)
tasks=[
asyncio.ensure_future(fun1()),
asyncio.ensure_future(fun2())
]
loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
在上述代碼中,@asyncio.coroutin為一個(gè)裝飾器,使用該裝飾器后,一個(gè)函數(shù)由普通函數(shù)成為了協(xié)程函數(shù)。與之前兩種方式相比,這段代碼實(shí)現(xiàn)了協(xié)程遇到I/O阻塞時(shí)自動(dòng)的切換。
四、async和await關(guān)鍵字實(shí)現(xiàn)方式
使用async和await關(guān)鍵字的實(shí)現(xiàn)方式,是Python3.5版本以后引入的協(xié)程操作方式,這種方式與第三種本質(zhì)上是相同的,但是由于這兩個(gè)關(guān)鍵字的引入,因此協(xié)程的實(shí)現(xiàn)比起第三種更加簡(jiǎn)介。這種實(shí)現(xiàn)方式Python代碼如下所示:
import asyncio
async def fun1():
print(1)
await asyncio.sleep(2)
print(2)
async def fun2():
print(3)
await asyncio.sleep(2)
print(4)
tasks=[
asyncio.ensure_future(fun1()),
asyncio.ensure_future(fun2())
]
loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
這種協(xié)程的實(shí)現(xiàn)方式,是Python官方推薦的實(shí)現(xiàn)方式,如果Python版本允許,盡量采取這種方式運(yùn)行。上述代碼執(zhí)行結(jié)果如下所示:
原文鏈接:https://blog.csdn.net/weixin_40228200/article/details/128610706
相關(guān)推薦
- 2022-09-02 Docker下Redis集群(主從+哨兵)安裝配置的實(shí)現(xiàn)步驟_docker
- 2023-04-26 C++利用伴隨陣法實(shí)現(xiàn)矩陣求逆_C 語言
- 2022-02-11 idea package合在一起,利用Compact Middle Packages解決 &
- 2022-03-22 C++this指針詳情_C 語言
- 2022-05-23 Python學(xué)習(xí)之時(shí)間包使用教程詳解_python
- 2022-05-08 Python與C語言分別完成排序流程_python
- 2022-09-02 Python用matplotlib庫畫圖中文和負(fù)號(hào)顯示為方框的問題解決_python
- 2022-04-18 python?commands模塊的適用方式_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支