繁体   English   中英

如何在不同的函数中启动和中断 while 循环?

[英]How to Start and Break a While Loop in Different Functions?

@bot.command()
async def start(ctx):
    channel = bot.get_channel(#*****)
    while True:
        if x >= 5:
            await channel.Send("Hi")
            time.Sleep(1)

@bot.command()
async def stop(ctx):
    #Something to stop the while True loop in the 'start' function

我想知道是否有办法通过在“停止”function 中写入一些内容来停止“开始”function 中的 while 循环。

你可以尝试这样的事情:

run_loop = True

@bot.command()
async def start(ctx):
    global run_loop
    channel = bot.get_channel(#*****)
    while run_loop:
        if x >= 5:
            await channel.Send("Hi")
            time.Sleep(1)

@bot.command()
async def stop(ctx):
    global run_loop
    run_loop = False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM