multi-threading question

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
a-ha
Posts: 21
Joined: Sat Jun 27, 2020 10:42 pm

multi-threading question

Post by a-ha » Mon Jun 29, 2020 4:54 am

Does the _thread module take advantage of the dual cores of esp32?
If I create 2 threads, will they be scheduled to be running on separate core?

thanks
a-ha

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

Re: multi-threading question

Post by pythoncoder » Mon Jun 29, 2020 5:48 am

I'm pretty sure the answer is no: MicroPython runs on one core and FreeRTOS on the other.
Peter Hinch

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

Re: multi-threading question

Post by jimmo » Mon Jun 29, 2020 6:02 am

That's right. At startup a FreeRTOS task is created for MicroPython, pinned to core 1, and all threads are also pinned to this core.

From memory the most recent discussion of this topic was here -- https://github.com/micropython/micropython/issues/4895

a-ha
Posts: 21
Joined: Sat Jun 27, 2020 10:42 pm

Re: multi-threading question

Post by a-ha » Mon Jun 29, 2020 7:30 am

Ok I see. FreeRTOS is used by micropython? Nice. Is this only for the esp32 board ? or for all cpu's?

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

Re: multi-threading question

Post by jimmo » Mon Jun 29, 2020 12:42 pm

Just ESP32.

User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Re: multi-threading question

Post by atmuc » Sun Feb 21, 2021 10:36 am

Does MicroPython still work on one core of ESP32?

bertel
Posts: 27
Joined: Tue Feb 09, 2021 3:55 pm
Location: Tokyo

Re: multi-threading question

Post by bertel » Sun Feb 21, 2021 3:44 pm

Even under vanilla Python, thread would use only one core ...

User avatar
atmuc
Posts: 18
Joined: Fri Nov 13, 2020 11:44 am
Location: Istanbul

Re: multi-threading question

Post by atmuc » Sun Feb 21, 2021 3:54 pm

https://www.youtube.com/watch?v=9vvobRfFOwk

according to this video, Raspberry pi pico uses 2 cores with MicroPython. I should buy pico when it is available in my country.

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

Re: multi-threading question

Post by pythoncoder » Sun Feb 21, 2021 3:55 pm

still applies. Given that the ESP32 runs FreeRTOS this is probably the most efficient way to use its two cores.

In general threading and Python don't coexist too well because of the GIL: this limits the benefit of running on multiple cores. If there is a single Python runtime, threads can cause each other to stall. I think there are nonstandard Python implementations which aim to resolve this, but I have no real knowledge of them.

A MicroPython exception is the Raspberry Pi Pico where two independent MicroPython runtimes can coexist, one on each core. This is magic, and the way to go :D

In MicroPython cooperative multitasking running on one core is more efficient than threading on one core.
Peter Hinch

bertel
Posts: 27
Joined: Tue Feb 09, 2021 3:55 pm
Location: Tokyo

Re: multi-threading question

Post by bertel » Sun Feb 21, 2021 5:08 pm

I don’t have any experience with threading in micropython, but according to the docs, _thread in micropython works just like in vanilla Python. There, threading uses the same core. To use multiple cores, we need to use multiPROCESSING. Most likely, the Swiss guy was tricked into believing threading uses multiple cores

Post Reply