I believe I am closing all of my ports properly, but it seems something may not be exiting properly.
Is it possible to run a "clean" command when my script starts to avoid this issue if it everything wasn't closed properly on the last exit? The only thing that I'm able to do to fix the problem is to reboot Linux.
My code is below, let me know if I am not exiting properly. I'm a beginner with both Python and Phidgets.
Code: Select all
def run():
try:
# io is a list which will hold all the channels
# Only purpose of io is to close them all at the end
io = []
LEDs_1 = init_LEDs(DEVICE_2_SERIAL_NUMBER)
io += LEDs_1
LEDs_2 = init_LEDs(DEVICE_1_SERIAL_NUMBER)
io += LEDs_2
vin_1 = init_vin(DEVICE_1_SERIAL_NUMBER, front=True)
io += vin_1
vin_2 = init_vin(DEVICE_2_SERIAL_NUMBER, front=False)
io += vin_2
toggle_1 = init_toggle(DEVICE_1_SERIAL_NUMBER)
io += [toggle_1]
toggle_2 = init_toggle(DEVICE_2_SERIAL_NUMBER)
io += [toggle_2]
# Flash LED's based on sensors until keyboard interrupt
while True:
for i in LEDS_A:
LEDs_1[i].setState(bool_a_1)
LEDs_2[i].setState(bool_a_2)
for i in LEDS_B:
LEDs_1[i].setState(bool_b_1)
LEDs_2[i].setState(bool_b_2)
time.sleep(1)
close_ports(io)
return 0
# A bunch of except blocks are the only way to exit
# All of them are supposed to close all the ports on the way out
# The KeyboardInterrupt functionality doesn't really work
except PhidgetException as e:
print("\nExiting with error(s)...")
DisplayError(e)
traceback.print_exc(file=open("sensor.log","w"))
print("Closing sensor channel")
close_ports(io)
return 1
except EndProgramSignal as e:
print(e)
print("Closing sensor channel")
close_ports(io)
return 1
except KeyboardInterrupt:
close_ports(io)
return 1
except RuntimeError as e:
close_ports(io)
print("Runtime Error: \n\t" + e)
traceback.print_exc(file=open("sensor.log","w"))
return 1
# Iterate through a list of channels and close them all
def close_ports(ch_list):
for ch in ch_list:
if ch: # if the channel isn't "None"
ch.close() # close the channel
run()