possible to use cython?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
seandepagnier
Posts: 3
Joined: Tue Feb 25, 2020 5:10 pm

possible to use cython?

Post by seandepagnier » Tue Feb 25, 2020 6:11 pm

Is it possible to pre-compile python scripts to work in the micropython environment the same as pre-compiled c modules?

The goal would be to run programs that use too much memory or are too slow otherwise.

User avatar
jimmo
Posts: 1465
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: possible to use cython?

Post by jimmo » Tue Feb 25, 2020 11:10 pm

Unsure specifically about Cython as I think it has a lot of things that are designed for CPython, so you'd have to set up bindings and types differently.

However, MicroPython kind of already has it's own Cython like mode, which is the native code emitter. So if you add the @micropython.native decorator to a function, it generates native code. Cython will obviously still be faster because it gets the benefit of GCC's amazing optimizer etc, but compared to just regular Python bytecode, @native can be a lot faster.

Also are you aware of .mpy files? You can pre-compile your Python code on your PC, then just copy the compiled bytecode to the device. However (as long as you set the -march flag) you can also use the @micropython.native decorator, and the generated .mpy will have native code instead for those functions. (You can also use "-X emit=native" to force the entire .mpy to be native code, regardless of whether the decorator is used).

Note that native code, while heaps faster, takes up a lot more RAM. Look into freezing them into the firmware if you want to avoid your bytecode/native code using up RAM.

stanely
Posts: 40
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: possible to use cython?

Post by stanely » Wed Feb 26, 2020 5:47 pm

Is the use of "-X emit=native" possible for the ESP32? The only choices I see in mpy_cross for architectures are:

-march=<arch> : set architecture for native emitter; x86, x64, armv6, armv7m, armv7em, armv7emsp, armv7emdp, xtensa, xtensawin

ESP32 is not in the list. How would I do that on my Windows development machine? I tried both xtensa and xtensawin, neither worked. I'll open a new topic on my problem, but am very interested in this one as well.

User avatar
jimmo
Posts: 1465
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: possible to use cython?

Post by jimmo » Wed Feb 26, 2020 10:17 pm

I replied on the other thread (viewtopic.php?f=2&t=7849) but for other people reading this, esp32=xtensawin.

Post Reply