打开新线程 运行 请求和 aiohttp.ClientSession 异步 IO 之间的时间成本差异?
Time cost difference between opening a new thread to run requests and aiohttp.ClientSession for async IO?
我知道 aiohttp 支持异步 IO,所以它是完全单线程的。但是 run_in_executor 有点像开始了一个新线程。但是我测试了一个1000次下载的任务,似乎差别不大。但我认为 aiohttp 应该更快,因为线程成本。我是不是做错了什么?
async def get(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
print(url, resp.status)
print(url, await resp.text())
loop = asyncio.get_event_loop()
tasks = [
get("http://www.google.com"),
get("http://www.google.com")
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
async def get_via_thread(url):
loop = asyncio.get_event_loop()
try:
response = await loop.run_in_executor(None, functools.partial(requests.get, url=url))
But I tested for a task with 1000 downloads, it seems the difference
is rather insignificant.
问题可能出在您的基准测试中。很难说具体在哪里,因为你没有提供一个来重现:)
例如,您可以查看 where OP tried to compare threads and coroutines and got no difference and ,其中解释了此结果并提供了修复。
我知道 aiohttp 支持异步 IO,所以它是完全单线程的。但是 run_in_executor 有点像开始了一个新线程。但是我测试了一个1000次下载的任务,似乎差别不大。但我认为 aiohttp 应该更快,因为线程成本。我是不是做错了什么?
async def get(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
print(url, resp.status)
print(url, await resp.text())
loop = asyncio.get_event_loop()
tasks = [
get("http://www.google.com"),
get("http://www.google.com")
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
async def get_via_thread(url):
loop = asyncio.get_event_loop()
try:
response = await loop.run_in_executor(None, functools.partial(requests.get, url=url))
But I tested for a task with 1000 downloads, it seems the difference is rather insignificant.
问题可能出在您的基准测试中。很难说具体在哪里,因为你没有提供一个来重现:)
例如,您可以查看