So device
is a threaded HidDevice
class. As you point out, raising an exception in this threaded object won't get caught by the handler (your do_GET
).
However, I'm wondering if raising an exception is really easiest thing to do (exceptions tend to be reserved for errors and problems). To achieve your aims, is it possible to use a global
variable and do something like this;
global e_value
e_value = None
def scales_handler(data):
print("Byte 0: {0}".format(data[0]))
print("Byte 1: {0}".format(data[1]))
print("Byte 2: {0}".format(data[2]))
print("Byte 3: {0}".format(data[3]))
print("Byte 4: {0}".format(data[4]))
print("Byte 5: {0}".format(data[5]))
print("All: {0}".format(data))
if data[1] == 4:
e_value = data[4] + (256 * data[5]))
devices = []
try:
for index, device in enumerate(all_hids):
if device.serial_number == requested_serial_number:
devices.append(device)
device.open()
device.set_raw_data_handler(sample_handler)
while True:
time.sleep(0.1)
finally:
for device in devices:
device.close()
if e_value is not None:
self.do_HEAD(200)
self.send_body(e_value)
I can't attest to what your devices are, so I should warn that this isn't thread safe - if more than one device has data[1] == 4
you will only have e_value
set by the last device to access it. (Though fixing this would be simple with a global array and counter object).