Executing MicroPython program in non-blocking fashion

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
mvladic
Posts: 3
Joined: Sun Jan 30, 2022 1:22 pm

Executing MicroPython program in non-blocking fashion

Post by mvladic » Sat Feb 05, 2022 2:47 pm

If I call "mp_call_function_0(...)" my program is blocked until that function returns (i.e. when MP program finishes). Is it possible to execute MP program in non-blocking fashion? Something like "mp_call_function_0_start(...)" and it returns immediately and later I call "mp_call_function_0_continue(...)" multiple times until it says that MP code execution is done.

I already asked this question in Forum / The MicroPython Language / General Discussion and Questions, but didn't get any valuable reply (probably wrong thread for these type of questions), so I later posted more clarification what I want to accomplish (slightly edited here):

This is more of a question how to run embedded MicroPython interpreter inside some firmware and not how to write MicroPython code in non-blocking way. I don't want my firmware to be blocked while python interpreter is running. I could use for example FreeRTOS and run MP interpreter in another thread, but if I have single threaded firmware I'm wondering is it possible at the same time to run both MicroPython interpreter and some other firmware code independent from MicroPython interpreter.

envirocoder
Posts: 4
Joined: Tue Nov 30, 2021 5:49 pm

Re: Executing MicroPython program in non-blocking fashion

Post by envirocoder » Tue Feb 08, 2022 6:04 pm

If I think I've understood what you want to do, the simple answer is no unfortunately. :(

One way I think about MicroPython is it is essentially a function that runs the VM and then that calls native C functions. If you want to 'pause' that function so you can do other things, then it's the same as any other premptive multiprocessing implementation with time slicing. You have to 'freeze time' for that function and preserve state so it can be continued. While it wouldn't be too difficult to do this scheduler part of the VM implementation, how would you pause the native C functions that the VM would call -- what happens if one of these blocked e.g. on input?

Essentially, you'd need to have chunks 'yield' to return after a short period and then when you recalled the function, continue the next chunk.

Using another thread is probably less painful.

Post Reply