B
B
bluefasik2022-01-03 23:50:05
Python
bluefasik, 2022-01-03 23:50:05

How to stop a thread when a command is entered?

def start_parser(message):
    if db.get_temp_country(message.from_user.id) == 'pool':
        pool = PoolUi()
        thread_counter = Thread(target=pool.generate_link).start()
        thread_counter = Thread(target=loading_advs(message).start()

I have a function in a telegram bot that launches a parser from another class "PoolUi", together with it another function is launched that shows the loading process. How to make the parser exit when you enter the "/stop" command, and the bot can continue to work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-04
@Vindicar

The only sane way is that the body of the thread should check some condition from time to time (be it the threading.Event object, or just a boolean variable). Accordingly, if the condition is met, the body of the thread must immediately end without finishing what has been started.
If you do not control what happens in the body of the thread, and you cannot divide its work into separate small iterations (to check the condition in between), then there is no good solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question