所以我写了这个脚本:
def schedule_setup():
# KILL OLD THREADS SHOULD THEY EXIST
global active
active = False
time.sleep(3)
active = True
global threadlist
threadlist = []
try:
sql = "SELECT TIME_TO_RUN FROM time_table"
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
#row[0].strftime('%H:%M')
t = threading.Thread(target=th,args=(row[0].strftime('%H:%M'),))
t.start()
threadlist.append(t)
# JOIN all threads to main memory
#for count in threadlist:
#count.join()
sql = "UPDATE motor SET UPDATE_SCHEDULE = 0"
try:
cursor.execute(sql)
# commit the changes in database
db.commit()
except:
# Rollback in case there is any error
print "no worky"
db.rollback()
except:
print "Error: UNABLE TO GET TABLE DATA"
它需要在 sql 上设置时间并创建计划的事件来执行操作。我将所有活动线程的线程“杀手”放在开头,这样如果我再次调用这个线程,因为时间已经更新,它可以杀死旧线程并用新线程替换它们。这一切都按照我的意愿进行,但是一旦调用该操作,整个程序就会崩溃……这是它调用的代码:
def th(run_time):
global active
schedule.every().day.at(run_time).do(run_motor)
while active == True:
schedule.run_pending()
time.sleep(1)
看看线程如何每秒检查一次?因此,当我尝试创建新线程时线程被杀死,但是当调用“run_motor”时,应该无限循环的主程序随后崩溃,有时其他线程仍在运行,所以这对我来说很奇怪。