How to define ctrl-C handler in MicroPython?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
liudr
Posts: 87
Joined: Tue Oct 17, 2017 5:18 am

How to define ctrl-C handler in MicroPython?

Post by liudr » Sun Apr 08, 2018 1:29 am

For cpython, this is what I have that handles ctrl-C:

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)
MicroPython complains about the last line. Is there any way to do similar things in MicroPython besides enclosing the entire script with a try? Thanks.

OutoftheBOTS_
Posts: 301
Joined: Mon Nov 20, 2017 10:18 am

Re: How to define ctrl-C handler in MicroPython?

Post by OutoftheBOTS_ » Sun Apr 08, 2018 2:41 am

Not sure if there is a way without using try

I pretty much use at least try and finally in every single Micro-Python script because on exit (whether it is an error for just script finishing) resources that were allocated during the script need to be released, like SPI , I2C objects or any other objects. If your script creates a SPI object and doesn't release it then you rerun your script it will create another instance of the SPi object not reuse the one all ready allocated. When you run a Cpython script from the command line i.e python Logger.py when it exits it should auto de-allocate all resources as it exits from python back to the command line. In Micro-Python when the script exits then it returns to the repl not the command line so nothing is de-allocated.

User avatar
pythoncoder
Posts: 2584
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to define ctrl-C handler in MicroPython?

Post by pythoncoder » 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?
Peter Hinch

User avatar
liudr
Posts: 87
Joined: Tue Oct 17, 2017 5:18 am

Re: How to define ctrl-C handler in MicroPython?

Post by liudr » Sun Apr 15, 2018 3:14 am

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?
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.

User avatar
pythoncoder
Posts: 2584
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to define ctrl-C handler in MicroPython?

Post by pythoncoder » Sun Apr 15, 2018 8:06 am

This shoud work on any MicroPython platform

Code: Select all

def foo():
    while True:
        pass

try:
    foo()
except KeyboardInterrupt:
    print('Got ctrl-c')
finally:
    # Optional cleanup code
Peter Hinch

User avatar
dhylands
Posts: 2755
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: How to define ctrl-C handler in MicroPython?

Post by dhylands » Tue Apr 17, 2018 2:33 am

I often put something like the following in my program as well:

Code: Select all

repl_uart = pyb.UART(6, 115200)
and then in my main loop:

Code: Select all

    if repl_uart.any():
        byte = repl_uart.readchar()
        if byte == 3:  # Control-C
            log('Control-C')
            raise KeyboardInterrupt

Post Reply