簡體   English   中英

打開新線程以運行請求與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))

但是我測試了一個下載1000次的任務,看來差別不大。

問題可能在您的基准測試中。 很難說確切的位置,因為您沒有提供要復制的內容:)

例如,您可以看一個最近的問題 ,OP嘗試比較線程和協程,但沒有區別,並回答了該結果,並提供了解決方案。

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM