想用协程试试访问网站结果遇到问题.
@types.coroutine
def urlopen(url):
html = yieldurllib.request.open(url)
return html
async def query(url):
response = await urlopen(url)
html = response.read()[:100]
print(html)
运行报错:
TypeErro: A Future, a coroutine or an awaitable is required.
我之前是直接在query()里面使用await urllib.request.urlopen()的,结果也报这个错,后来就改成把访问 url 用types.coroutine变成成协程,可是还是报这个错。
所以想请教一下。 我想知道的是,我的思路是错的还是说实现的方式有问题。 感谢!