S
S
Studentka19962021-06-17 04:37:26
Python
Studentka1996, 2021-06-17 04:37:26

How to stop the current thread?

global t_annul
    def worker(id_order, date_order):
        t_annul = threading.currentThread()
        while getattr(t_annul, 'do_run', True): # запускаю счетчик
            print (f"working on {id_order}, {date_order}")
            time.sleep(20) # если время не прошло, жду
            conn_DB.upd_statusOrders('Аннулирован', id_order)
            bot.send_message(message.chat.id, text = f'*\u3030ЗАКАЗ №{id_order} АННУЛИРОВАН* от {date_order} на сумму {zakaz[3]} руб.',
                parse_mode = "Markdown", reply_markup = send_keyb_order(buttons.ls_keybs_orders(message.chat.id)[2]))
            t_annul.do_run = False # остановка потока

 if zakaz[0] == 'Оплата онлайн':
                t_annul = threading.Thread(target=worker, args=(f"{conn_DB.id_Zakaz()}", f"{zakaz[2]}"))
                t_annul.start()
 if 'ОтменаЗаказа' in text:
        t_annul.do_run = False


I have tried various solutions and all with no success. The thread is stopped, but only in the worker() function, how can I stop an already running thread using the CancelOrder button? Please help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Studentka1996, 2021-06-18
@Studentka1996

import multiprocessing
import time


  def send_annul(sec, id_order, date_order, sum_order):
        print('Заказ №' + id_order)
        time.sleep(sec) # через 15 сек отправляю сообщение # если время не прошло, жду
        conn_DB.upd_statusOrders('Аннулирован', int(id_order))

    def worker():
        global proc
        # proc = multiprocessing.Process(target=worker(f"{conn_DB.id_Zakaz()}", f"{zakaz[2]}", f"{zakaz[4]}"))
        proc = multiprocessing.Process(target=send_annul, args=(10, f"{conn_DB.id_Zakaz()}", 1, 1))
        print(proc)
        proc.start()
        # proc.terminate()

worker()

  if 'ОтменаЗаказа' in text:
        print('остановка потока')
        print(proc)
        proc.terminate()

F
Fenrir89, 2021-06-17
@Fenrir89

like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question