源码如下, 总是运行不了报错
<code type='Python'>
import aiohttp
import asyncio
async def main(pool): # aiohttp必须放在异步函数中使用
tasks = []
sem = asyncio.Semaphore(pool) # 限制同时请求的数量
[tasks.append(control_sem(sem, 'https://api.github.com/events?a={}'.format(i))) for i in range(10)] # 十次请求
await asyncio.wait(tasks)
async def control_sem(sem, url): # 限制信号量
async with sem:
await fetch(url)
async def fetch(url):
async with aiohttp.request('GET', url) as resp:
json = await resp.json()
print(json)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(pool=2))
</code>
报错信息如下
Traceback (most recent call last):
File "C:/Users/Administrator/.PyCharmCE2019.1/config/scratches/Demo3.py", line 298, in <module>
import aiohttp
File "D:\Program Files\Python3.7.2\lib\site-packages\aiohttp__init__.py", line 6, in <module>
from .client import (
File "D:\Program Files\Python3.7.2\lib\site-packages\aiohttp\client.py", line 32, in <module>
from . import hdrs, http, payload
File "D:\Program Files\Python3.7.2\lib\site-packages\aiohttp\http.py", line 7, in <module>
from .http_parser import (
File "D:\Program Files\Python3.7.2\lib\site-packages\aiohttp\http_parser.py", line 14, in <module>
from .base_protocol import BaseProtocol
File "D:\Program Files\Python3.7.2\lib\site-packages\aiohttp\base_protocol.py", line 4, in <module>
from .tcp_helpers import tcp_nodelay
File "D:\Program Files\Python3.7.2\lib\site-packages\aiohttp\tcp_helpers.py", line 20, in <module>
def tcp_keepalive(transport: asyncio.Transport) -> None:
AttributeError: module 'asyncio' has no attribute 'Transport'