multithreading - Python Threads. Serial Port. KeyboardInterrupt -
i'm implementing multithreaded program 1 of threads reads serial port. i'm having trouble when trying end program keyboardinterrupt. have main thread , 1 reads serial port. code looks this:
import threading import time def reader(name, port, run_event): while run_event.is_set(): port.read(1) #process data............... if __name__ == "__main__": run_event = threading.event() run_event.set() port=serial.serial(port=args.serialport,baudrate=921600) port.flush() port.flushinput() t1 = threading.thread(target = timed_output, args = ("reader",port,run_event)) t1.start() try: while 1: time.sleep(.1) except keyboardinterrupt: print "bye bye" run_event.clear() t1.join() port.close() print "threads closed"
sometimes works fine other times not. of logs.
why 1 not caught try-except block?
^ctraceback (most recent call last): file "bbbdaq.py", line 147, in <module> time.sleep(0.1) keyboardinterrupt
two interrupts first 1 takes out of try-expect block , while executing print statement?
^ctraceback (most recent call last): file "bbbdaq.py", line 149, in <module> print 'bye bye' keyboardinterrupt
at point confused. can explain happens there , missing.
-----------edit_1----------------
ive changed try-except block this:
def signal_handler(signal, frame): print 'bye bye' run_event.clear() t1.join() port.close() sys.exit(0) signal.signal(signal.sigint,signal_handler) while 1: time.sleep(0.1)
most of times program exits expected when ctrl-c hit, other times im getting log:
^cbye bye bye bye exception systemexit: 0 in <module 'threading' '/usr/lib/python2.7/threading.pyc'> ignored
is exception fired twice, how posible?
Comments
Post a Comment