Asynciorun_until_complete报错RunTimeError。
创始人
2024-09-21 07:00:37
0

在升级 Python 版本后,该问题通常会被解决。但若是不想升级 Python 版本,可以根据以下方案解决:

将代码中存在阻塞的方法,使用线程或进程池进行异步调用,比如使用 run_in_executor() 方法。

示例:

import asyncio
import concurrent.futures

async def main():
  with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
      loop = asyncio.get_event_loop()
      tasks = [
          loop.run_in_executor(executor, blocking_function_1, args_1),
          loop.run_in_executor(executor, blocking_function_2, args_2)
      ]
      await asyncio.gather(*tasks)

def blocking_function_1(args_1):
    # 阻塞的方法1

def blocking_function_2(args_2):
    # 阻塞的方法2

此外,也可以使用 asyncio.run() 方法替代 run_until_complete() 方法,它是 Python 3.7 中引入的新方法。

示例:

import asyncio    

async def foo():
    # 异步的代码逻辑

async def main():
    await asyncio.gather(foo())

asyncio.run(main())

相关内容

热门资讯

透视教学"德普之星怎... 透视教学"德普之星怎么设置埋牌"详细辅助2025版教程(辅助透视软件技巧)科技教程也叫必备教程,这是...
透视透视"wepok... 这是一款非常优秀的wepoker黑侠破解 ia辅助检测软件,能够让你了解到wepoker黑侠破解中牌...
透视挂透视"xpok... 透视挂透视"xpoker怎么作弊"详细辅助爆料教程(的辅助器攻略);人气非常高,ai更新快且高清可以...
透视线上"哈糖大菠萝... 1、透视线上"哈糖大菠萝有挂吗5个常用方法"详细辅助技巧教程(作弊神器);详细教程。2、哈糖大菠萝有...
透视安卓版"hhpo... 《透视安卓版"hhpoker到底可以作弊码"详细辅助大神讲解(私人局俱乐部辅助技巧)》 hhpoke...
透视app"wepo... 透视app"wepoker代打辅助机器人"详细辅助爆料教程(私人局有透视技巧)是一款可以让一直输的玩...
透视app"wepo... 透视app"wepoker辅助器下载"详细辅助介绍教程(免费辅助器技巧)这是由厦门游乐互动科技有限公...
透视中牌率"拱趴大菠... 1、透视中牌率"拱趴大菠萝万能挂"详细辅助插件教程(透视底牌技巧)(UU poker、拱趴大菠萝万能...
透视存在"wepok... 透视存在"wepoker一直输的号能继续打吗"详细辅助玩家教程(辅助软件价格神器)科技教程也叫必备教...
透视好友房"xpok... 透视好友房"xpoker辅助怎么用"详细辅助分享教程(私人局俱乐部软件)1、许多玩家不知道xpoke...