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

1、先調(diào)用函數(shù)獲取生成器對象,再調(diào)用next方法或send(None)方法打開coroutine。
2、函數(shù)執(zhí)行到y(tǒng)ield位置,返回yield后掛起,把控制流交回主線程。再調(diào)用send法時,可以傳輸數(shù)據(jù)并激活協(xié)程。
繼續(xù)執(zhí)行到最后或下一個yield語句。
實例
""" # 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的運行過程,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:pythoncoroutine的運行過程
文章網(wǎng)址:http://m.fisionsoft.com.cn/article/dpjicds.html


咨詢
建站咨詢
