我正在探索异步http请求的aiohttp。
aiohttp 网站上的客户端快速启动建议将此代码作为一个最小的示例:
import aiohttp
async with aiohttp.ClientSession() as session:
async with session.get('http://httpbin.org/get') as resp:
print(resp.status)
print(await resp.text())对于我来说,python 3.6.5的结果是
使用aiohttp.ClientSession()作为会话的异步: SyntaxError:无效语法
我是不是漏掉了什么?
任何帮助都是非常感谢的!谢谢。
编辑:
我在做测试。首先,我意识到我需要python 3.7。所以我切换了,现在错误消息是:
使用aiohttp.ClientSession()作为会话的异步: SyntaxError:“具有”外部异步函数的异步
发布于 2018-11-01 21:12:18
问题不在于Python : aiohttp支持Python3.5.3,所以3.6.5是绝对好的。但请注意错误消息:
SyntaxError:“具有”外部异步函数的异步
就是这样:您只能在异步函数中使用async with:
import aiohttp
import asyncio
async def start():
async with aiohttp.ClientSession() as session:
async with session.get('http://httpbin.org/get') as resp:
print(resp.status)
print(await resp.text())
loop = asyncio.get_event_loop()
loop.run_until_complete(start())这条消息对于Python3.5和3.6是正确的,而且,他们只在3.7中使它更加友好。
https://stackoverflow.com/questions/53105866
复制相似问题