简体   繁体   中英

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

I would like to know if there is a way to stop the while loop in the 'start' function by writting something in the 'stop' function.

You can try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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