我想使用 python 并行运行许多 HTTP 请求。我尝试使用 asyncio 这个名为 aiohttp 的模块。
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
for i in range(10):
async with session.get('https://httpbin.org/get') as response:
html = await response.text()
print('done' + str(i))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)
我期望它并行执行所有请求,但它们是一一执行的。虽然,我后来使用线程解决了这个问题,但我想知道这有什么问题吗?