asyncio.wait在一个协程因异常停止时,可能不会停止具有websocket调用的另一个协程。
创始人
2024-09-21 06:00:55
0

可以使用try/except块捕获协程中的异常,并通过调用协程对象的cancel方法来取消未完成的协程。例如:

async def coro1():
    try:
        # websocket call
    except:
        # catch exception and cancel other coroutines
        task.cancel()

async def coro2():
    # some other code

async def main():
    task1 = asyncio.create_task(coro1())
    task2 = asyncio.create_task(coro2())
    done, pending = await asyncio.wait(
        [task1, task2],
        return_when=asyncio.ALL_COMPLETED
    )

    for task in pending:
        task.cancel()

asyncio.run(main())

相关内容

热门资讯

透视教材!hhpoker怎么防... 透视教材!hhpoker怎么防作必弊,hhpoker可以开挂吗(透视)本来有挂(哔哩哔哩)hhpok...
透视妙招!aapoker插件下... 透视妙招!aapoker插件下载,aapoker辅助软件合法吗(透视)原来存在有透视插件(哔哩哔哩)...
透视指南书!竞技联盟破解版最新... 透视指南书!竞技联盟破解版最新版,德普之星透视辅助插件(透视)原来真的是有挂(哔哩哔哩)德普之星透视...
透视妙计!德普辅助软件,德普之... 透视妙计!德普辅助软件,德普之星透视(透视)确实存在有透视工具(哔哩哔哩)1、德普之星透视透视辅助软...
透视模板!拱趴大菠萝挂怎么安装... 透视模板!拱趴大菠萝挂怎么安装,hhpoker辅助(透视)本来真的有挂(哔哩哔哩)1、透视模板!拱趴...
透视课程!epoker免费透视... 透视课程!epoker免费透视脚本,拱趴大菠萝万能挂(透视)好像是有脚本技巧(哔哩哔哩)1、用户打开...
透视窍门!aapoker辅助怎... 透视窍门!aapoker辅助怎么用,wepoker怎么设置透视(透视)竟然真的有挂(哔哩哔哩)小薇(...
透视烘培!epoker底牌透视... 透视烘培!epoker底牌透视,德州局透视脚本下载安装最新版本(透视)竟然有透视攻略(哔哩哔哩)该软...
透视办法!we poker免费... 透视办法!we poker免费辅助器,wepoker私局辅助(透视)一直是有挂(哔哩哔哩)1、玩家可...
透视妙计!wepoker有辅助... 透视妙计!wepoker有辅助插件吗,wepoker手机版辅助(透视)一直有脚本工具(哔哩哔哩)1)...