我创建了一个电报机器人。我启动按钮上的计时器。以下是以下代码:
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!")
当我在计时器过期后单击按钮时,我得到“线程只能运行一次”,这很有意义,因为我没有多线程版本的代码。如何使多线程选项,使计时器可以无限期地启动
从threading docs开始:
完成后,
thread.is_alive()
再次返回False
,您试图再次运行它,但失败了。您需要初始化一个新的MyThread
对象才能再次运行它相关问题 更多 >
编程相关推荐