新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:pythoncoroutine的運(yùn)行過(guò)程
說(shuō)明

1、先調(diào)用函數(shù)獲取生成器對(duì)象,再調(diào)用next方法或send(None)方法打開(kāi)coroutine。
2、函數(shù)執(zhí)行到y(tǒng)ield位置,返回yield后掛起,把控制流交回主線程。再調(diào)用send法時(shí),可以傳輸數(shù)據(jù)并激活協(xié)程。
繼續(xù)執(zhí)行到最后或下一個(gè)yield語(yǔ)句。
實(shí)例
""" # BEGIN CORO_AVERAGER_TEST >>> coro_avg = averager() # <1> >>> next(coro_avg) # <2> >>> coro_avg.send(10) # <3> 10.0 >>> coro_avg.send(30) 20.0 >>> coro_avg.send(5) 15.0 # END CORO_AVERAGER_TEST """ def averager(): total = 0.0 count = 0 average = None while True: # <1> term = yield average # <2> total += term count += 1 average = total/count
以上就是python coroutine的運(yùn)行過(guò)程,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:pythoncoroutine的運(yùn)行過(guò)程
分享網(wǎng)址:http://m.5511xx.com/article/dpjicds.html


咨詢
建站咨詢
