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

Question:

@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.

Asked By: ChanHY

||

Answers:

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
Answered By: PApostol