Page 1 of 2

multi-threading question

Posted: Mon Jun 29, 2020 4:54 am
by a-ha
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

Re: multi-threading question

Posted: Mon Jun 29, 2020 5:48 am
by pythoncoder
I'm pretty sure the answer is no: MicroPython runs on one core and FreeRTOS on the other.

Re: multi-threading question

Posted: Mon Jun 29, 2020 6:02 am
by jimmo
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

Re: multi-threading question

Posted: Mon Jun 29, 2020 7:30 am
by a-ha
Ok I see. FreeRTOS is used by micropython? Nice. Is this only for the esp32 board ? or for all cpu's?

Re: multi-threading question

Posted: Mon Jun 29, 2020 12:42 pm
by jimmo
Just ESP32.

Re: multi-threading question

Posted: Sun Feb 21, 2021 10:36 am
by atmuc
Does MicroPython still work on one core of ESP32?

Re: multi-threading question

Posted: Sun Feb 21, 2021 3:44 pm
by bertel
Even under vanilla Python, thread would use only one core ...

Re: multi-threading question

Posted: Sun Feb 21, 2021 3:54 pm
by atmuc
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.

Re: multi-threading question

Posted: Sun Feb 21, 2021 3:55 pm
by pythoncoder
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.

Re: multi-threading question

Posted: Sun Feb 21, 2021 5:08 pm
by bertel
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