asyncio 抛出运行时错误并忽略异常

res*_*est 7 python-3.x python-asyncio aiohttp

下面是一个收集 URL 长度的简单程序。

import aiohttp
import asyncio
from time import perf_counter


URLS = ['http://www.cnn.com', 'http://www.huffpost.com', 'http://europe.wsj.com',
        'http://www.bbc.co.uk', 'http://failfailfail.com']

async def async_load_url(url, session):
    try:
        async with session.get(url) as resp:
            content = await resp.read()
        print(f"{url!r} is {len(content)} bytes")
    except IOError:
        print(f"failed to load {url}")        

async def main():

    async with aiohttp.ClientSession() as session:
        tasks = [async_load_url(url, session) for url in URLS]
        await asyncio.wait(tasks)

if __name__ == "__main__":
    start = perf_counter()
    asyncio.run(main())
    elapsed = perf_counter() - start
    print(f"\nTook {elapsed} seconds")

Run Code Online (Sandbox Code Playgroud)

为什么以下代码在 python 3.9 中失败并出现运行时错误并忽略异常?如何修复它?

回溯是:RuntimeError: Event loop is closed特别是Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001F8A7A713A0>

Mat*_*ler 6

这是由 Windows 上 aiohttp 的一个已知问题引起的,有关详细信息,请查看https://github.com/aio-libs/aiohttp/issues/4324上的错误

有一些技巧可以消除此错误。第一种方法是获取事件循环并调用,run_until_complete而不是asyncio.run(main())像这样:

asyncio.get_event_loop().run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

或者,将事件循环策略更改为WindowsSelectorEventLoopPolicy调用之前asyncio.run(main())也可以正常工作,因为在使用WindowsProtractorEventLoopPolicy.

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)

当然,第二种解决方案将使您的代码平台特定,所以要小心。