我创建了一个电报机器人。我在按钮上启动计时器。以下是以下代码:
class MyThread(Thread):
def __init__(self, event):
Thread.__init__(self)
def run(self):
time.sleep(5)
print("my thread")
stopFlag = Event()
thread = MyThread(stopFlag)
@bot.message_handler(content_types=['text'])
def buttons(message):
if message.chat.type == 'private':
if message.text == 'Запуск таймера 1':
if thread.is_alive():
bot.send_message(message.chat.id, "wait for the timer to end")
else:
thread.start()
bot.send_message(message.chat.id, "timer started!")
当我在计时器到期后单击按钮时,我得到“线程只能运行一次”,这是有道理的,因为我没有代码的多线程版本。如何制作多线程选项,以便可以无限期地启动计时器?