您不能return
在Python 2或Python 3.0-3.2中使用值来退出生成器。您需要使用 不 带表达式的yield
加号:return
__
if response.error:
self.error("Error while retrieving the status")
self.finish()
yield error
return
在循环本身中,yield
再次使用:
for line in response.body.split("\n"):
if line != "":
#net = int(line.split(" ")[1])
#sens = int(line.split(" ")[2])
#stype = int(line.split(" ")[3])
value = int(line.split(" ")[4])
print value
yield value
return
替代方法是引发异常或使用龙卷风回调。
在Python 3.3和更高版本中,return
生成器函数中带有值会导致该值附加到StopIterator
异常中。对于async def
异步生成器(Python 3.6及更高版本),return
必须仍然没有价值。