[英]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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.