相关疑难解决方法(0)

在 python 3.8 中使用 aiohttp 和 asyncio 关闭异常事件循环

我正在使用 asyncio 和 aiohttp 来发出并发请求。我最近将 Python 升级到了 3.8.0 版,并且RuntimeError: Event loop is closed在程序运行后得到了一个。

import asyncio
import aiohttp

async def do_call(name, session):
    async with session.get('https://www.google.be') as response:
        await response.text()
        return 'ok - {}'.format(name)

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [do_call(str(i), session) for i in range(0,4)]
        results = await asyncio.gather(*tasks)
        print(results)

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

我确实从 asyncio.gather() 得到了一个有效的结果,但是在退出时会引发异常。我想更改代码,以免遇到异常。

回溯如下:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001E9A92079D0>
Traceback (most recent call last):
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File …
Run Code Online (Sandbox Code Playgroud)

python async-await python-asyncio aiohttp

5
推荐指数
1
解决办法
5159
查看次数

标签 统计

aiohttp ×1

async-await ×1

python ×1

python-asyncio ×1