Multithreading of mBot2 / CyberPi?


#1

Is there any example how to run code multithreaded?

Let’s say play music, listening to voice commands while run line following program?


#2

In Python there is probably better… It seems the chip allows it. It’s not great for music but it works.


#3

Thanks for reply. Do you mean that if I need a multithreaded tasks I have to make some separate forever loops running from CyberPi start and they will run in parallel asynchronously?


#4

Hi Showman,

Look at the generated code. I think we are talking about 2 separate event loops for block programming. In Python, I didn’t check. However, to answer your question, the ESP32 can perform Multitread tasks.
2022-01-15%2008_46_03-Window

Makeblock will tell you more, I’m sure.


#5

I haven’t asked Makeblock, so this is speculation, but I believe each block group that starts with an event block is a separate thread in Python. However, all threads are pinned to the same core, and there’s also the Global Interpreter Lock, so the threads run concurrently on one core, not in parallel on multiple cores.

This means it’s safe to share global variables between threads.

Here’s what I found about multithreading with MicroPython on ESP32 (although it mentions FreeRTOS, not CyberOS):
https://forum.micropython.org/viewtopic.php?t=7776
https://forum.micropython.org/viewtopic.php?f=18&t=8610


#6

@FluffusCatus @Showman @Crackel Yes, it is sage and easy to multithread with mBot Neo. And yes, you can use multiple variables across threads.