You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10环境下使用asyncio与aiohttp出现RuntimeError: Event loop is closed错误的求助

RuntimeError: Event loop is closed with asyncio and aiohttp on Windows 10

Hey everyone,

I know this question might have been asked a hundred times already, but I still can't figure out why my code keeps throwing an error at the very end. I'm running this on Windows 10, and I'm totally new to asyncio—so I thought I was doing everything right, but clearly something's off. Could someone give me a gentle nudge in the right direction?

Here's my code:

import asyncio
import aiohttp

async def read_index_page():
    async with aiohttp.ClientSession() as session:
        async with session.get('https://some/api/url') as resp:
            print(resp.status)

async def main():
    loop = asyncio.get_event_loop()
    task = loop.create_task(read_index_page())
    await asyncio.gather(task)

if __name__ == '__main__':
    asyncio.run(main())

This code triggers the well-known RuntimeError: Event loop is closed error. I'm stuck here and would really appreciate any help.

Thanks,
Thomas

内容的提问来源于stack exchange,提问作者brillenheini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:53:12