如何停止另一个功能?

时间:2020-09-06 10:14:27

标签: python function telegram-api

TOKEN = 'token'
bot = telebot.TeleBot(TOKEN)

def main():
   for i in range(0,100):
      print(i)

@bot.message_handler(commands=['start'])
def start(message):
  main()

@bot.message_handler(commands=['stop'])
def stopfunc(message):
   #how to stop the function main() ?

while True:
   bot.polling()

1 个答案:

答案 0 :(得分:0)

添加停止标志:

  • 向主功能添加逻辑:当停止标志为True时,主功能应返回
  • 在stopfunc中将stop标志设置为True
stop = False
def main():
   global stop
   for i in range(0,100):
       if stop:
          break
       print(i)
    
@bot.message_handler(commands=['stop'])
def stopfunc(message):
    global stop
    stop = True       

...