Python 如何使用命令中断此循环

Python 如何使用命令中断此循环,python,bots,discord.py,Python,Bots,Discord.py,我有这个密码。它读取一个文件。我希望能够在discord上使用stop命令来停止循环 @client.command() async def read(ctx, message): with open(f'{message}.txt', 'r') as f: for line in f: print(line, end="") await ctx.send(f"{line}")

我有这个密码。它读取一个文件。我希望能够在discord上使用stop命令来停止循环

@client.command()
async def read(ctx, message):
    with open(f'{message}.txt', 'r') as f:
        for line in f:
            print(line, end="")
            await ctx.send(f"{line}")
            await asyncio.sleep(3)

您可以添加一个名为
is\u stopped
的全局变量,它将是一个布尔值。在循环的每次迭代中,它将检查
是否已停止
是否为真。我的意思是:

全局已停止
is_stopped=False
@client.command()
异步def停止(ctx):
全球行动停止了
是否停止=正确
等待ctx.send(“停止!”)
@client.command()
异步def读取(ctx,消息):
将open(f'{message}.txt',r')作为f:
对于f中的行:
如果未停止,请执行以下操作:
打印(第行,结束=“”)
等待ctx.send(f“{line}”)
等待asyncio.sleep(3)
其他:
打破