So I wrote this script:
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"
It takes times setup on sql and creates a scheduled event to do an action. I put in the beginning the thread "killer" for all active threads so that if I ever call this thread again because times have been updated it can kill old ones and replace them with the new. It all works how I want it to, but as soon as the action is called the whole program crashes... here is the code it calls:
def th(run_time):
global active
schedule.every().day.at(run_time).do(run_motor)
while active == True:
schedule.run_pending()
time.sleep(1)
see how the thread checks every second? So threads are killed when I try to create new ones, but when "run_motor" gets called, afterwords the main program that is supposed to loop indefinitely crashes and sometimes other threads are still going so it is all very strange to me.