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

编程入门 行业动态 更新时间:2024-10-28 16:24:02
本文介绍了在python 3.8中使用aiohttp和asyncio关闭异常事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用asyncio和aiohttp发出并发请求.我最近将Python升级到了3.8.0版,并且在程序运行后得到了 RuntimeError:事件循环已关闭.

I am using asyncio and aiohttp to make concurrent requests. I've recently upgraded Python to version 3.8.0 and I'm getting a RuntimeError: Event loop is closed after the program has run.

import asyncio import aiohttp async def do_call(name, session): async with session.get('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())

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

I do get a valid result from asyncio.gather(), but when exiting the exception is raised. I'd like to change the code so it doesn't run into exceptions.

回溯如下:

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 "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 711, in call_soon self._check_closed() File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 504, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed`

推荐答案

我认为这很可能是aiohttp错误.具体来说,我在他们的github上发现了此问题: github/aio-libs/aiohttp/issues/4324

I think this is most likely an aiohttp bug. Specifically, I found this issue on their github: github/aio-libs/aiohttp/issues/4324

我知道这不一定对您有帮助,但是也许您可以转回去,不再用头撞墙.您的代码很好!

I realise this doesn't necessarily help you, but maybe you can switch back and stop banging your head against a wall. Your code is fine!

更多推荐

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

本文发布于:2023-11-23 09:43:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   事件   python   asyncio   aiohttp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!