【发布时间】:2021-07-31 03:37:00
【问题描述】:
我正在尝试在 python 中编写一个警报,它有 6 个需要多线程的函数。其中5个是警报,其中一个显示时间。只要选择菜单选项以及警报响铃时,线程需要启动和停止。显示线程是唯一一直运行到程序停止的线程。我当前的警报代码如下所示(为了清楚起见,我删除了很多)
class TAlarm1 (threading.Thread):
def Alarm1():
while True:
#code which keeps running until the time is equal to the input given (expected to thread)
thread1 = threading.Thread(target=TAlarm1)
thread1.start()
def AlarmSelector():
print("Select an Alarm") #5 alarms will be added however each one accomplishes the same task. all of them need to run simultaneously
choice = int(input())
if choice == 1:
ala = TAlarm1()
ala.Alarm1()
if choice == 6:
DisplayTime() #goes back to displaying time
每当我运行此代码时,程序都不会显示任何错误,但它不会运行 TAlarm1() 中的代码。 我该如何解决这个问题?
【问题讨论】:
-
你为什么要创建一个
Thread子类而不覆盖run方法,然后将该类用作threading.Thread 的目标参数?那么你永远不会使用thread1。您的意图有点模糊,看起来您可能已经以please fix everything that is wrong with this的形式提出了一些问题。
标签: python python-3.x multithreading python-multithreading