I have a python script in which I'm using threading to wait for inputs on my raspberry pi. This is the first time I've ever used threading before and I'm having some difficulties handling the KeyboardInterrupt. Its not really an issue, but if the user presses control-c to early, like right as soon as the program starts, python freaks out and spews a console full of import errors. Ive tried wrapping the imports in try - excepts, but that doesn't seem to work. I have a feeling its something with the threading as I've never had this issue before.
Besides wrapping everything in try-excepts i also tried:
from threading import Thread
cond = threading.Condition(threading.Lock())
cond.acquire()
cond.wait(None)
which I hear was suppose to not allow the keyboard interrupt until its been loaded?
Is there anyway just to not have python do anything on keyboardinterrupts?
Any help would be appreciated. Thanks!
My Code:
import RPi.GPIO as GPIO
import pymysql as mysql
import time
import urllib.request
from threading import Thread
GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()
class table():
__slots__ = ['pin','num','time']
def __init__(self, pin, num, time=0):
self.pin = pin
self.num = num
self.time = time
# Room ID
room = 6
# time (minutes)
thetime = 38
# table Setup
tableList = [ \
table(11,6), \
table(13,2), \
table(15,4), \
table(19,5), \
table(21,3), \
table(23,1), \
]
def settable(table):
with urllib.request.urlopen("http://time.zyphox.com") as url:
curtime = int(url.read())+(thetime*60)
con = mysql.connect(host="345", user="435", passwd="345435", db="34534543")
cur = con.cursor()
cur.execute("UPDATE tables SET time='"+str(curtime)+"' WHERE number='"+str(table)+"' AND id_room='"+str(room)+"'")
cur.close()
con.close()
print("Setting table " + str(table) + " to " + str(thetime) + " minutes...")
def watchtable(table):
GPIO.setup(table.pin, GPIO.IN)
while True:
if(GPIO.input(table.pin)!=1 and table.time <= time.time()):
settable(table.num)
table.time = time.time() + 10
def main():
print("Loading kitchen System...")
for table in tableList:
t = Thread(target=watchtable,args=(table,))
t.daemon=True
t.start()
time.sleep(1)
print("Successful!")
while True:
time.sleep(300)
print("- Pinging MYSQL database.")
main()