I started learning python using the example of telegtam bot wrote this code

@bot.message_handler(commands=['help', 'start']) def send_welcome(message): msg = bot.send_message(message.chat.id, 'Привет! Я Bot') bot.get_updates bot.polling(none_stop=True, interval=0) 

handler is the incoming message when prompted by the user. Now commands commands=['help', 'start'] are caught and the answer to the bot follows. The last line makes the code work constantly.

As soon as I add a function that constantly pings the computer

 def ping_Test(): while True: response = subprocess.Popen(["ping", "-n", "1", "-w", "200", "192.168.0.32"]).wait() if response != 0: bot.send_message('667252555', 'Выключен') time.sleep(5) ping_Test() 

There is a problem, the function works fine, that is, it sends a bot message to the bot, but the bot stops responding to the commands, that is, I enter \ start and the welcome message does not come.

As soon as I remove the function, everything works. How to make the bot continue to work in parallel? Where am I wrong?

  • 2
    The bot has no time to communicate with the server, since ping_Test () does not return, permanently stuck in the perpetual loop. Help either streams or asynchrony. - Sergey Gornostaev

1 answer 1

Solved the asynchronous problem

 from telebot.util import async @bot.message_handler(commands=['help', 'start']) @async() def send_welcome(message): msg = bot.send_message(message.chat.id, 'Привет! Я FenixITbot, работаю в Фениксе.') bot.get_updates @async() def ping_Test(): while True: response = subprocess.Popen(["ping", "-n", "1", "-w", "200", "192.168.0.32"]).wait() if response == 0: bot.send_message('66725218', 'Включен') time.sleep(5) ping_Test()