-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Threads
The _thread module provides the ability to create additional threads and is intended to implement the API defined by the equivalent CPython module. Threading is particularly desirable for microcontrollers with multiple cores in order to achieve greater performance. However, the feature is currently ''experimental'', poorly documented and differs between ports.
The higher-level threading module is not yet implemented but cant be built on top of the _thread
primitives.
Note that an asyncio solution is often easier to implement, portable and more robust and should be preferred where concurrency (not necessarily parallelism) is required.
Threads are currently supported on ESP32, RP2040 (Pico), STM32, and Unix.
The ESP32 port is built on the ESP IDF which uses FreeRTOS to manage threading.
Threads are currently created on the same core as the MicroPython interpreter.
See mpthreadport.c, specifically how xTaskCreatePinnedToCore
is used rather than xTaskCreate
which would allow FreeRTOS to choose the appropriate core to run on.
There are some complexities that prevent the second core to be used (additional information required!).
The RP2040 is a dual-core microcontroller. MicroPython supports running a single extra thread, which runs on the second core.
Note that this port does not use the GIL, otherwise the two cores would run in lock-step, providing no benefit from threading. This means that all program state must be carefully synchronised.
We provide additional firmware variants for the Pyboard that enables the _thread
module. It allows multiple concurrent threads sharing the single core with pre-emptive context switching.
- Threads in MicroPython: Damien's talk that introduced the feature
- Threading: A doc which aims to explain the different forms of MicroPython concurrency.