• <small id='chaYf'></small><noframes id='chaYf'>

    1. <tfoot id='chaYf'></tfoot>

    2. <legend id='chaYf'><style id='chaYf'><dir id='chaYf'><q id='chaYf'></q></dir></style></legend>
        • <bdo id='chaYf'></bdo><ul id='chaYf'></ul>
        <i id='chaYf'><tr id='chaYf'><dt id='chaYf'><q id='chaYf'><span id='chaYf'><b id='chaYf'><form id='chaYf'><ins id='chaYf'></ins><ul id='chaYf'></ul><sub id='chaYf'></sub></form><legend id='chaYf'></legend><bdo id='chaYf'><pre id='chaYf'><center id='chaYf'></center></pre></bdo></b><th id='chaYf'></th></span></q></dt></tr></i><div id='chaYf'><tfoot id='chaYf'></tfoot><dl id='chaYf'><fieldset id='chaYf'></fieldset></dl></div>

        首次完成后取消两个异步协程

        时间:2023-09-10

        <i id='fgwfK'><tr id='fgwfK'><dt id='fgwfK'><q id='fgwfK'><span id='fgwfK'><b id='fgwfK'><form id='fgwfK'><ins id='fgwfK'></ins><ul id='fgwfK'></ul><sub id='fgwfK'></sub></form><legend id='fgwfK'></legend><bdo id='fgwfK'><pre id='fgwfK'><center id='fgwfK'></center></pre></bdo></b><th id='fgwfK'></th></span></q></dt></tr></i><div id='fgwfK'><tfoot id='fgwfK'></tfoot><dl id='fgwfK'><fieldset id='fgwfK'></fieldset></dl></div>

            <small id='fgwfK'></small><noframes id='fgwfK'>

            <tfoot id='fgwfK'></tfoot>

                <bdo id='fgwfK'></bdo><ul id='fgwfK'></ul>
                  <tbody id='fgwfK'></tbody>

                <legend id='fgwfK'><style id='fgwfK'><dir id='fgwfK'><q id='fgwfK'></q></dir></style></legend>
                1. 本文介绍了首次完成后取消两个异步协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在用 Python 制作一个 Discord 机器人.我想做的是等待两个协程,一旦第一个完成运行其余代码但取消剩余的协程(不像 here 我只有一个协程并希望在特定时间后取消它.)p>

                  这是最小的可重现示例:

                  导入异步导入不和谐客户端 = 不和谐.客户端()异步定义测试函数():等待 asyncio.sleep(10)print("10 秒.")异步def testfunc2():等待 asyncio.sleep(15)打印(15 秒.")@client.eventasync def on_ready():打印(已连接!")等待 asyncio.wait([testfunc(),testfunc2()],return_when=asyncio.FIRST_COMPLETED)client.run('我的机器人令牌')

                  我打算在这个最小示例中发生的事情是打印10 seconds".10 秒后,然后不打印任何其他内容.然而,实际发生的是它也打印出15 seconds".15 秒后,我想取消那个协程,这样就不会发生.

                  在这种情况下,我似乎不能使用 asyncio.wait_for,因为有两个协程而不是一个,这两个协程必须同时运行,直到一个完成.由于 asyncio.wait 完成后不会取消协程,有什么办法可以取消它们?

                  解决方案

                  如果将协程包装在任务中,可以取消未完成的协程

                  @client.eventasync def on_ready():打印(已连接!")任务 = [asyncio.create_task(testfunc()), asyncio.create_task(testfunc2())]等待 asyncio.wait(任务,return_when=asyncio.FIRST_COMPLETED)对于任务中的任务:如果不是 task.done():任务.取消()

                  I am making a Discord bot in Python. What I want to do at one point is to await two coroutines, and once the first has completed run the rest of the code but cancel the remaining coroutine (unlike here where I just had one coroutine and wanted it to be cancelled after a specific time.)

                  Here is the minimum reproducible example:

                  import asyncio
                  import discord
                  
                  client = discord.Client()
                  
                  async def testfunc():
                      await asyncio.sleep(10)
                      print("10 seconds.")
                  
                  async def testfunc2():
                      await asyncio.sleep(15)
                      print("15 seconds.")
                  
                  @client.event
                  async def on_ready():
                      print("Connected!")
                      await asyncio.wait([testfunc(),testfunc2()],return_when=asyncio.FIRST_COMPLETED)
                  
                  client.run('MY BOT TOKEN')
                  

                  What I intend to happen in this minimal example is for it to print "10 seconds." after 10 seconds and then not print anything else. However, what actually happens is that it also prints "15 seconds." after 15 seconds, and I want to cancel that coroutine so that does not happen.

                  It appears that I cannot use asyncio.wait_for in this case as there are two coroutines instead of one, both of which must run simultaneously until one finishes. Since asyncio.wait does not cancel coroutines after one has finished, is there any way to cancel them?

                  解决方案

                  If you wrap the coroutines in tasks, you can cancel the ones that haven't finished

                  @client.event
                  async def on_ready():
                      print("Connected!")
                      tasks = [asyncio.create_task(testfunc()), asyncio.create_task(testfunc2())]
                      await asyncio.wait(tasks,return_when=asyncio.FIRST_COMPLETED)
                      for task in tasks:
                          if not task.done():
                              task.cancel()
                  

                  这篇关于首次完成后取消两个异步协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Discord.py-Rewrite 在出现未知命令或其他错误时发送 下一篇:Discord.py ctx.guild.edit 有效,但 self.bot.guild.edit 无效

                  相关文章

                  最新文章

                  <legend id='xytlB'><style id='xytlB'><dir id='xytlB'><q id='xytlB'></q></dir></style></legend>
                    <bdo id='xytlB'></bdo><ul id='xytlB'></ul>

                  1. <tfoot id='xytlB'></tfoot>

                  2. <small id='xytlB'></small><noframes id='xytlB'>

                    <i id='xytlB'><tr id='xytlB'><dt id='xytlB'><q id='xytlB'><span id='xytlB'><b id='xytlB'><form id='xytlB'><ins id='xytlB'></ins><ul id='xytlB'></ul><sub id='xytlB'></sub></form><legend id='xytlB'></legend><bdo id='xytlB'><pre id='xytlB'><center id='xytlB'></center></pre></bdo></b><th id='xytlB'></th></span></q></dt></tr></i><div id='xytlB'><tfoot id='xytlB'></tfoot><dl id='xytlB'><fieldset id='xytlB'></fieldset></dl></div>