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())

相关内容

热门资讯

透视好友房!pokernow辅... 透视好友房!pokernow辅助工具(透视)原来真的是有辅助app(有挂技术)-哔哩哔哩1、poke...
据通报!wepoker数据分析... 据通报!wepoker数据分析(透视)确实有辅助插件(有挂技术)-哔哩哔哩1、起透看视 wepoke...
今天上午!wepoker免费辅... 今天上午!wepoker免费辅助器(透视)一直真的有辅助下载(有挂规律)-哔哩哔哩1、下载好wepo...
透视ai代打!wepoker游... 透视ai代打!wepoker游戏下载(透视)一直是真的辅助安装(有挂秘诀)-哔哩哔哩透视ai代打!w...
今天下午!wepoker有没有... 今天下午!wepoker有没有机器人(透视)本来是真的辅助平台(有挂分析)-哔哩哔哩1、wepoke...
有了最新消息!aapoker公... 有了最新消息!aapoker公共底牌(透视)一直存在有辅助插件(有挂方针)-哔哩哔哩1、进入游戏-大...
透视实锤!德州局透视脚本免费版... 透视实锤!德州局透视脚本免费版下载手机版(透视)竟然真的有辅助插件(有挂方法)-哔哩哔哩一、德州局透...
透视私人局!uupoker透视... 透视私人局!uupoker透视(透视)一直真的有辅助工具(真是有挂)-哔哩哔哩1、首先打开uupok...
透视脚本!newpoker脚本... 透视脚本!newpoker脚本(透视)果然存在有辅助软件(真的有挂)-哔哩哔哩运newpoker脚本...
今天下午!wpk真的有透视嘛(... 今天下午!wpk真的有透视嘛(透视)确实真的是有辅助修改器(有挂透明挂)-哔哩哔哩1、wpk真的有透...