I have a question that has been bothering me for a long time. I would greatly appreciate your help!
Let's imagine a situation like this:
You entered a function by a command. In this particular function, there is a loop that takes a long time to finish, or worse, it is actually an infinite loop. The question is, how can you abort this loop or function so that the bot can handle other requests?
An example would be like this:
def function(update, context):
counter = 0
message = update.message.reply_text(str(counter))
while True:
counter += 1
message = message.edit_text(text = str(counter))
time.sleep(1)
dispatcher.add_handler(CommandHandler("main", function))
After user enters this function by "/main", he or she gets a message that includes a number, which the number increases every second and this will take forever to finish.
Something that I have tried:
Design another
MessageHandler
, if user sends some keywords like "stop", it changes the value of a global variable. In the loop, it checks if this variable isTrue
every time before it adds the number, if it wasFalse
, it breaks.However, the MessageHandler won't detect this message before the loop has actually ended, so this is meaningless.
Include a
InlineKeyboard
in the message, with a button called "Stop". Design anotherCallbackQueryHandler
to handle this button.Similarly, CallbackQueryHandler will only receive the callback after the loop was finished. This can be verified if I set a manual
break
when the counter hits a number like 5 or something.
I was thinking if I could design some kind of abort function that runs asynchronously, but I am personally not very familiar with async in Python. After some trials like setting async def
or CallbackQueryHandler(query_handle, run_async = True)
, I still failed to produce this effect.
I noticed there is a decorator called from telegram.ext.dispatcher import run_async
, does that help? Or, is there any other way to realize this result?
Thanks!