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

相关内容

热门资讯

WPK私人局攻略!哈糖大菠萝助... WPK私人局攻略!哈糖大菠萝助手,智星菠萝德州ai多功能辅助器软件1、很好的工具软件,可以解锁游戏的...
WPK真假烘培!拱趴大菠萝挂怎... WPK真假烘培!拱趴大菠萝挂怎么安装,智星菠萝德州ai多功能辅助器攻略1、上手简单,内置详细流程视频...
WPK工具资料!AApoker... WPK工具资料!AApoker安装包,AApokerai多功能辅助器技巧1、全新机制【AApoker...
WPK作弊方案!佛手在线大菠萝... WPK作弊方案!佛手在线大菠萝技巧,WPKai多功能透视挂插件1、让任何用户在无需佛手在线大菠萝技巧...
WPK工具方案!AApoker... WPK工具方案!AApoker万能,aapokerai多功能辅助技巧一、AApoker万能游戏安装教...
WPK购买操作!约局吧是否有挂... WPK购买操作!约局吧是否有挂,智星菠萝德州ai多功能透视挂教程小薇(辅助器软件下载)致您一封信;亲...
WPK俱乐部指南!拱趴大菠萝有... WPK俱乐部指南!拱趴大菠萝有什么挂,德扑之星ai多功能开挂app1、拱趴大菠萝有什么挂脚本辅助下载...
WPK控制牌手段!德扑辅助ap... WPK控制牌手段!德扑辅助app,德扑之星ai多功能开挂技巧1、玩家可以在德扑辅助app线上大神俱乐...
WPK购买总结!aapoker... WPK购买总结!aapoker辅助器怎么用,WPKai多功能开挂工具1、下载好aapoker辅助器怎...
WPK方法讲义!aapoker... WPK方法讲义!aapoker透视脚本,WePoKerai多功能开挂神器1、实时aapoker透视脚...