同时运行异步任务

时间:2020-09-12 10:35:29

标签: python python-3.x python-asyncio

我刚开始使用AsyncIO,但是我很难理解其背后的一些机制。根据文档,要同时运行任务,我需要使用asyncio.gather(),所以这是我尝试的方法:

async def nested():
    BU = cfscrape.create_scraper()
    URL = "https://www.binance.com/api/v3/exchangeInfo"
    ResultRaw = BU.get(URL, timeout=(10, 15)).content
    Result = json.loads(ResultRaw)
    print('Done')
    return Result

async def main():
    await asyncio.gather(nested(), nested(), nested())

asyncio.run(main())

我希望这段代码可以同时输出Done, Done, Done,但是它将同步执行该功能。这是为什么?是因为我试图在nested内部执行同步代码吗?

0 个答案:

没有答案