相关疑难解决方法(0)

使用 asyncio.run() 时关闭 Asyncio 事件循环

我开始使用 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())
Run Code Online (Sandbox Code Playgroud)

这是输出:

raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我会收到这个错误,有人可以帮我解决这个问题吗?

python python-3.x python-asyncio

9
推荐指数
2
解决办法
715
查看次数

标签 统计

python ×1

python-3.x ×1

python-asyncio ×1