Code: Select all
def SIGINT_handler(signal, frame):
ser.close()
data_file.close()
print('Quitting program!')
sys.exit(0)
signal.signal(signal.SIGINT, SIGINT_handler)
Code: Select all
def SIGINT_handler(signal, frame):
ser.close()
data_file.close()
print('Quitting program!')
sys.exit(0)
signal.signal(signal.SIGINT, SIGINT_handler)
Sorry, ESP32 port. I want to have a graceful way to end a script that is running on repl. It's not essential that I had a way to do so. It's on my good-to-have list. Thanks.pythoncoder wrote: ↑Sun Apr 08, 2018 7:04 am@liudr You didn't mention what platform you're running on. MicroPython is primarily targeted at microcontrollers. The concept of a signal is about communication between processes: irrelevant if you're running a single threaded program on bare metal. On bare metal exception handling is the way to intercept ctrl-c.
On the Unix build there is a use case for signal processing. The implementation in micropython-lib looks as if it should handle SIGINT. If you're running the Unix build what error message are you seeing?
Code: Select all
def foo():
while True:
pass
try:
foo()
except KeyboardInterrupt:
print('Got ctrl-c')
finally:
# Optional cleanup code
Code: Select all
repl_uart = pyb.UART(6, 115200)
Code: Select all
if repl_uart.any():
byte = repl_uart.readchar()
if byte == 3: # Control-C
log('Control-C')
raise KeyboardInterrupt