参照aiohttp 官网的教程,写了个示例,发现语法报错。
import asyncio
import aiohttp
async with aiohttp.ClientSession() as session:
async with session.get('https://api.github.com/events') as resp:
print(resp.status)
print(await resp.text())
-------------
File "/Users/zed/PycharmProjects/example/used_aiohttp/1.py", line 3
async with aiohttp.ClientSession() as session:
^
SyntaxError: invalid syntax
现在是不支持这样的写法了吗
async with xxxxx as xxxx:
pass
1
freestyle Feb 10, 2017
得用 async def xxx(): 包起来
|
2
freestyle Feb 10, 2017
|
4
qsnow6 OP |
5
qsnow6 OP  |
6
qsnow6 OP ? |
7
qsnow6 OP  |
8
a87150 Feb 10, 2017
```
import asyncio import aiohttp async def fetch(): async with aiohttp.ClientSession() as session: async with session.get('https://api.github.com/events') as r: print(r.status) print(await r.text()) loop = asyncio.get_event_loop() loop.run_until_complete(fetch()) loop.close() ``` 我估计官网是默认你会用 asyncio |
9
a87150 Feb 10, 2017
原来回复不能用 markdown
|
10
LukeXuan Feb 10, 2017 via Android
其实我觉得挺需要一个 apython 直接在 async def 环境内执行. py 文件
|
11
chy373180 Feb 10, 2017
要写在 async def 内
|