【发布时间】:2021-12-02 10:09:24
【问题描述】:
我开始使用 AsyncIO 和 AioHTTP,我正在编写一些基本代码来熟悉语法。我尝试了以下应该同时执行 3 个请求的代码:
import time
import logging
import asyncio
import aiohttp
import json
from aiohttp import ClientSession, ClientResponseError
from aiocfscrape import CloudflareScraper
async def nested(url):
async with CloudflareScraper() as session:
async with session.get(url) as resp:
return await resp.text()
async def main():
URL = "https://www.binance.com/api/v3/exchangeInfo"
await asyncio.gather(nested(URL), nested(URL), nested(URL))
asyncio.run(main())
这是输出:
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
我不明白为什么会出现这个错误,谁能帮我解决这个问题?
【问题讨论】:
-
你在什么操作系统上运行代码?
-
我在 Windows 10,Python 3.8 上运行它
-
您好像遇到了this issue。但是很可能您的代码实际上正在运行,尽管消息难看 - 您只是从 main() 返回内容而不打印它,因此除了消息之外没有输出 other。跨度>
-
非常感谢!所以实际上是 aiohttp 引发了错误。我松了一口气,因为我不知道这个错误背后的原因
-
不,它来自
asyncio.ProactorEventLoop相关的内部asyncio组件,用于处理Windows 的IO 事件循环。我在异步 TCP 套接字服务器上遇到了同样的问题。
标签: python python-3.x python-asyncio