【发布时间】:2017-11-13 19:49:00
【问题描述】:
在asyncio 发布之前,我一直在使用基于生成器的协程。
现在我正在尝试学习 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'
【问题讨论】:
标签: python async-await