I
I
InternetMaster2021-10-08 21:57:03
Python
InternetMaster, 2021-10-08 21:57:03

How to make a Telegram message update while another script is running?

I have code on telebot. So, we need the bot to send a message and update it every second until the other script completes.

sek = bot.send_message(message.chat.id, '⌛ *Подождите, выполняю запрос*', parse_mode='Markdown')
sek
i = 0
while i == 0:
        time.sleep(1)
        bot.edit_message_text(chat_id=message.chat.id, message_id=sek.message_id, text='⏳ *Подождите выполняю запрос*', parse_mode='Markdown')
            time.sleep(1)
            bot.edit_message_text(chat_id=message.chat.id, message_id=sek.message_id, text='⌛ *Подождите, выполняю запрос*', parse_mode='Markdown'
api_get_den = msg
#И тут дальше скрипт который должен исполнятся.


As planned, I use the variable i which is by default = 0, while updates the message while i = 0, and when the script is executed, at the end the variable i is updated to a different value, and the while stops working. But the script is not executed further while the while loop is in effect.
This is an unwanted cycle, how to solve the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alan Gibizov, 2021-10-08
@InternetMaster

Here there is a section about asynchronous message delivery. Either change it to telebot.Asynctelebot as described in the instructions for the telebot, or switch to aiogram .

V
Vindicar, 2021-10-08
@Vindicar

Yes, oddly enough, while one part of the code is being executed, the other is not being executed. There are three exceptions to this rule:
1. Multithreading. Each thread of execution can execute code independently of the other. The OS itself will force them to interleave, almost nothing is required of the code to do. You can simply take existing single-threaded code and execute it on another thread. But the problem is that then two threads can interfere with each other when trying to access the same data ("race condition"). Avoiding this is not entirely trivial.
2. Multiprocessing. Almost the same as multithreading. Has some goodies, but much more difficult to use.
3. Asynchrony. In this case, one part of the code may pause its execution, "giving way" to another for a short while. Since the execution is strictly sequential, there will be no race condition. But on the other hand, both parts of the code must be able to cooperate with each other from the very beginning. This will require a complete redesign of the code, since you cannot just "take and screw" asynchrony to existing code. On the other hand, bots are often written using asynchronous code, so you may only need to rework your "working" script.
In general, the choice is between horseradish and radish. Without knowing how the bot is implemented and what the working script does, it is difficult to suggest something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question