runtimeerror:事件循环已关闭-Asyncio,aiohttp,并发请求
我是异步编程的新手。环境-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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论