exit from pyexec_file/pyexec_frozen_module

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
BramPeeters
Posts: 54
Joined: Wed Jan 31, 2018 3:10 pm

exit from pyexec_file/pyexec_frozen_module

Post by BramPeeters » Wed Jan 31, 2018 3:24 pm

Hi,

When running a never ending python program via pyexec_file/pyexec_frozen_module, is it somehow possible to 'exit' from that by doing a call from C land from eg another task (in a freertos environment).

The use case would be that eg a script runs that runs a certain control loop on a board, but then an updated script arrives that needs to replace the ongoing script.
i can probably kill and restart the entire freertos task, but it would be a lot cleaner if i could force a an exit of the python script from within C.

In pyexec.h I see
// Set this to the value (eg PYEXEC_FORCED_EXIT) that will be propagated through
// the pyexec functions if a SystemExit exception is raised by the running code.
// It will reset to 0 at the start of each execution (eg each REPL entry).
extern int pyexec_system_exit;

but if i understand correcly this seems to be more a variable that is set when an exit is triggered from within micropython.
Not something you set externally (and from a different task -> thread safety issues).

Any suggestions ?
If such functionality is not foreseen, would it be possible to add it without braking to much stuff :). Or would killing the freertos task be vastly more simple.... (hopefully in such i way that i can reclaim all memory etc)


Sorry if the question has been asked before, i tried searching the forum/google but no combination of keywords gave me something to go on.

Thanks,
Bram

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

Re: exit from pyexec_file/pyexec_frozen_module

Post by dhylands » Wed Jan 31, 2018 6:21 pm

You could use mp_sched_schedule. You'd pass in a function which raises an exception. This would then execute on the next byte code boundary. The only reason this wouldn't work would be if the micropython code was called into a C function which didn't return.

BramPeeters
Posts: 54
Joined: Wed Jan 31, 2018 3:10 pm

Re: exit from pyexec_file/pyexec_frozen_module

Post by BramPeeters » Wed Jan 31, 2018 9:38 pm

That looks like a nice solution, thank you very much!

Post Reply