runtimeerror:事件循环已关闭-Asyncio,aiohttp,并发请求

发布于 2025-02-14 01:45:59 字数 4020 浏览 0 评论 0原文

我是异步编程的新手。环境-Win11,Python 3.10.5。我正在尝试提出并发请求,并简单地在列表中获取结果。我已经尝试仔细研究Stackoverflow上的建议,以解决问题,但无法得到造成的建议。尽管我可以看到结果,但仍会获得RuntimeRor:事件循环已关闭。这是我的代码

import asyncio
import aiohttp
import time

names = ["rahul"]*4

def get_tasks(session):
    tasks = []
    for name in names:
        tasks.append(asyncio.create_task(session.get(url =f"https://api.agify.io?name={name}", ssl=False)))
    return tasks

async def my_f2():
    result = []
    async with aiohttp.ClientSession() as session:
        tasks = get_tasks(session)
        responses = await asyncio.gather(*tasks,return_exceptions=True)
        #print(responses)
        for response in responses:
            result.append(await response.json())
    # print(result)
    return result

start = time.time()
result = asyncio.run(my_f2())
print(result)
end = time.time()
print(f"Total time : {end-start}")

,这是显示结果列表的输出提出的例外

在 38,'count':9575},{'name':'rahul','age':38,'count':9575},, {'name':'rahul','age':38,'count':9575}]总时间: 0.989113807678227在以下方面忽略了例外: Trackback(最近的最新通话):文件“ C:\ Users \ Dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第116行, del self.close()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第108行,近距离 self.__loop.call_soon(self._call_connection_lost,none)文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ base_events.py”, 第750行,在call_soon中 self._check_closed()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ lib \ asyncio \ base_events.py”, 第515行,in _check_closed runtimeerror(“事件循环已关闭”)RuntimeError:事件循环已关闭: _proactorbasepipetransport。 Trackback(最近的最新通话):文件“ C:\ Users \ Dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第116行, del self.close()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第108行,近距离 self.__loop.call_soon(self._call_connection_lost,none)文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ base_events.py”, 第750行,在call_soon中 self._check_closed()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ lib \ asyncio \ base_events.py”, 第515行,in _check_closed runtimeerror(“事件循环已关闭”)RuntimeError:事件循环已关闭: _proactorbasepipetransport。 Trackback(最近的最新通话):文件“ C:\ Users \ Dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第116行, del self.close()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第108行,近距离 self.__loop.call_soon(self._call_connection_lost,none)文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ base_events.py”, 第750行,在call_soon中 self._check_closed()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ lib \ asyncio \ base_events.py”, 第515行,in _check_closed runtimeerror(“事件循环已关闭”)RuntimeError:事件循环已关闭: _proactorbasepipetransport。 Trackback(最近的最新通话):文件“ C:\ Users \ Dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第116行, del self.close()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ asyncio \ proactor_events.py”, 第108行,近距离 self.__loop.call_soon(self._call_connection_lost,none)文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python310 \ lib \ asyncio \ base_events.py”, 第750行,在call_soon中 self._check_closed()文件“ c:\ users \ dipankar dey \ appdata \ local \ program \ python \ python \ python310 \ lib \ lib \ asyncio \ base_events.py”, 第515行,in _check_closed RAIND TIMETERERR(“事件循环已关闭”)RuntimeError:事件循环已关闭

I am very new to async programming. Environment- Win11,Python 3.10.5. I am trying to make concurrent requests and obtain the results in a list simply. I've tried going through the suggestions on stackoverflow to troubleshoot the issue but couldn't get what exactly is causing it. Though I can see the result, still getting the Runtimeerror: Event loop is closed. Here is my code

import asyncio
import aiohttp
import time

names = ["rahul"]*4

def get_tasks(session):
    tasks = []
    for name in names:
        tasks.append(asyncio.create_task(session.get(url =f"https://api.agify.io?name={name}", ssl=False)))
    return tasks

async def my_f2():
    result = []
    async with aiohttp.ClientSession() as session:
        tasks = get_tasks(session)
        responses = await asyncio.gather(*tasks,return_exceptions=True)
        #print(responses)
        for response in responses:
            result.append(await response.json())
    # print(result)
    return result

start = time.time()
result = asyncio.run(my_f2())
print(result)
end = time.time()
print(f"Total time : {end-start}")

This is the exception raised with the output that shows the result list

[{'name': 'rahul', 'age': 38, 'count': 9575}, {'name': 'rahul', 'age':
38, 'count': 9575}, {'name': 'rahul', 'age': 38, 'count': 9575},
{'name': 'rahul', 'age': 38, 'count': 9575}] Total time :
0.9891138076782227 Exception ignored in: <function _ProactorBasePipeTransport.del at 0x00000234477E25F0> Traceback (most recent call last): File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 116, in del
self.close() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 108, in close
self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 750, in call_soon
self._check_closed() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 515, in _check_closed
raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed Exception ignored in: <function
_ProactorBasePipeTransport.del at 0x00000234477E25F0> Traceback (most recent call last): File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 116, in del
self.close() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 108, in close
self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 750, in call_soon
self._check_closed() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 515, in _check_closed
raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed Exception ignored in: <function
_ProactorBasePipeTransport.del at 0x00000234477E25F0> Traceback (most recent call last): File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 116, in del
self.close() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 108, in close
self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 750, in call_soon
self._check_closed() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 515, in _check_closed
raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed Exception ignored in: <function
_ProactorBasePipeTransport.del at 0x00000234477E25F0> Traceback (most recent call last): File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 116, in del
self.close() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py",
line 108, in close
self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Dipankar
Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 750, in call_soon
self._check_closed() File "C:\Users\Dipankar Dey\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py",
line 515, in _check_closed
raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文