B
B
bakin20042020-09-04 15:15:04
Python
bakin2004, 2020-09-04 15:15:04

How to stop the execution of all threads using the Telegram bot command?

I'm trying to create a Telegram bot in python. This bot, at the user's command, launches multithreading with different task execution cycles. I need to make it so that when the user enters a command (in this case it is /start) with a certain value, for example /start stop (the value of stop), it stops all threads, and then can successfully start them again with the command.

def startarg(arg):
    return arg.split()[1]

@bot.message_handler(commands=["start"])
def start(message):
    for i in range(1):
        try:
            start = startarg(message.text)
            bot.send_message(message.chat.id, 'Запущено')
        except:
            start = 'error'
            break
    if start == 'error':
        bot.send_message(message.chat.id, 'Вы не указали значение!')
    else:
        if start[0] == '1':
            start = '+'+start

    def some_0():
        while True:
            try:
            except:
                pass

    def some_1():
        while True:
            try:
            except:
                pass
                    
    def some_2():
        while True:
            try:
            except:
                pass

    th_0, th_1, th_2 = Thread(target=some_0), Thread(target=some_1), Thread(target = some_2)

    if __name__ == '__main__':
        th_0.start(), th_1.start(), th_2.start()
        th_0.join(), th_1.join(), th_2.join()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question