在asyncio
發佈之前,我一直使用基於生成器的協程。等待awaitable_object的值是多少?
現在我正在學習Python 3.5中引入的新功能async/await
。這是我的測試程序之一。
class Await3:
def __init__(self, value):
self.value = value
def __await__(self):
return iter([self.value, self.value, self.value])
async def main_coroutine():
x = await Await3('ABC')
print("x =", x)
def dummy_scheduler(cobj):
snd = None
try:
while True:
aw = cobj.send(snd)
#snd = 42
print("got:", aw)
except StopIteration:
print("stop")
dummy_scheduler(main_coroutine())
它的輸出是:
got: ABC
got: ABC
got: ABC
x = None
stop
的x
值是await awaitable_object
表達式的結果。爲什麼這個值是None
,我怎樣才能將它設置爲我想要的值?
我能找到的所有值是await couroutine()
的值取決於協程的返回值,但這不是我的情況。
取消註釋snd = 42
不起作用。該錯誤是AttributeError: 'list_iterator' object has no attribute 'send'