No video
Micropython Threads - Use Both Cores, on Raspberry Pi Pico and ESP32
Did you know you're probably only using just half the power of your Raspberry Pi Pico or ESP32? Lets look at how to use Threads in Micropython; it's easier than you'd think.
💁♂️ For more information on SMARS Robots, tutorials and more visit: www.smarsfan.com
🎖To join the membership at 🥉bronze, 🥈silver or 🥇gold levels, head over to
www.smarsfan.c...
Join the list - action.smarsfa...
☕️ Enjoy this video? Buy me a coffee! www.buymeacoff...
📸 Follow me on Instagram - @kevinmcaleer / kevinmcaleer
🐦 Follow me on Twitter - @kevsmac / kevsmac
🙂📘 Join the Facebook group - Small Robots / smallrobots
👩💻 Threads MicroPython Code on GitHub: www.github.com...
🎵 Music by Epidemic Sounds www.epidemicso...
#Pico #MicroPython #Threads
Пікірлер: 54
sincerely thank you for making this video. your whole channel has taught me so much i needed to know
@kevinmcaleer28
2 жыл бұрын
Thanks Christian!
@skyagnitti
6 ай бұрын
same here mate, this was EXACTLY what I was looking for to learn how to do multi-threading for my esp32 and rPiW microcontrollers :)
Oh boy! So glad I found this. I’ve just written my first threaded program. I needed to get input from a web form and use the entries to drive some peristaltic pumps (through a relay) for a set period of time. This FORTRAN 77 programmer is slowly getting his head round micro processors and Python. Fun stuff! I’ve subscribed and looking forward to learning a ton more from you.
Hi Kevin. You seem to be talking about only a single thread running per core. That's a very simple implementation. In general we would be running multiple threads per core. And yes, there is no Realtime Operating System (RTOS) in place on the ESP32 & Pico, so there has to be thread handling code within the micropython interpreter and associated libraries. I believe this is implemented using the Asyncio library. Have you thought about doing a video covering Asyncio in micropython? That would be helpful.
Solution to the dining philosophers problem: each rolls for initiative and awaits all initiative rolls to complete; if any two adjacent philosophers tie, they reroll to break the tie; assign an initiative ordering to each philosopher based upon the initiative roll and any tie breaks; once that process is complete, the highest initiative philisopher dines first, then whoever was not next to him and who had the highest initiative dines next, and they are removed from the next dining round, then repeat this process until all philosophers have dined.
Micropython on the esp32 supports multiple threads, but not multiple cores. All threads are run on the same core. The other core is dedicated to FreeRTOS.
@mattgiles6171
2 жыл бұрын
This is what I thought!
Best video for modern time processors/ controllers. was looking for using the triac firing which is in milliseconds and another loop for firebase databse. The asyncio was a bit complicated as the database request took some seconds and the triac waa in milliseconds turning on and off so the triac task(asyncio) was not giving up the control back to database loop time to time. Threading made it fun for me. Thanks from India.
@zhouyu9971
Жыл бұрын
I'm learning asyncio and found it's rather complicated if I just want the led blinks separately while the main code is running. I'll definitely try this and hope it solve the issue.
Thank you man, is what I was looking for !!!!!. Excelente !!!!
Thank you again, very clear and understandable video. I am working on IR receiver code where one core reads the TV remote control data and second core runs engines on my small robot. Thank you for your input ;)
Really good presentation and explanation of threads. Thanks
You really are the best explaining this topic, thank you very much for sharing the knowledge. Greetings from Argentina.
@kevinmcaleer28
Жыл бұрын
Thanks Aldo!
Thank you for sharing your knowledge with us. It gives me a great place to start with multi-threading.
@kevinmcaleer28
2 жыл бұрын
Thanks Stephen!
Hey Kelvin, this video is superb, actually I didn't know esp32 can process close to 17 threads (due to being used to two threads in Pico). Am a big fan of your tutorials.
@kevinmcaleer28
2 жыл бұрын
Thanks Alex! Glad they are useful
Thank you very much for this video, it really helped me on my proyects, it was one of the missing parts I needed. Great tutorial :)
Great video ,thank you for sharing
ESP32 uses FreeRTOS natively, as an element of SDK. So you can can create as many threads you like, at least until you run out of resources. Code executed on Raspberry Pi pico runs on bare metal. But nothing stops you from running FreeRTOS on one of the cores.
Thanks for the video
Thanks a lot, I was able to have a website (phew) managing a neopixel ring... Thank you thank you than you!
how about Raspberry Pi zero 2w ? Can it runs more threads??
Hey Kevin! I really liked this informative video as well as your friendly demeanor and inspiring enthusiasm. Keep up the good work, and have a new subscriber - on me! ;-)
@kevinmcaleer28
2 жыл бұрын
Thanks Terji!
an additional question: we can also use 'with mutex' at the beginning, which would do the acquire at the beginning and release at the end? It would be simpler in case we forget the 'release'? Many thanks
Thanks for the great introduction. Just to understand correctly which part of the program is running on core 0 and which part on core 1? You could run the LED off with a second thread and have nothing in the main loop? Is the code in the main loop always running on a distinct core? Many thanks.
Great video
@kevinmcaleer28
2 жыл бұрын
Thanks!
Q What is the current status of thread support in MicroPython & the Pico? Do we still onky have the ability to create 2 tasks? And how does thread support contrast with using the asyncio library? Appreciate any answers you can provide.
is threading in python really multicore programming or cooperative scheduling seems to mean different on pico vs esp32? there is no utility of writing python code that behaves differently on pico compared to esp32. really bad design, better off sticking to C/C++
Hi, great video! If you connect to a WiFi outside a thread (RPi Pico W) will the thread be able to access the internet connection? I'm having trouble on this.
Thanks for the excellent video. At around the 32 min mark, when you’re testing the number of threads, is it possible some of them are exiting before new ones are being created? What happens if you put a while true loop at the end of each threaded function to make sure it keeps running and doesn’t exit?
@kevinmcaleer28
2 жыл бұрын
Great suggestion I'll give that a try next time I've got an ESP32 connected.
Is it possible for the ESP32'cores run on different programing languages at the same time??? Say one with Micropython and the other with C or C++
Hi Kevin liking your pico videos, I am new to this microcontroler. I notice on time stamp 44.20 on the Micropython Threads - Use Both Cores, on Raspberry Pi Pico and ESP32 video, a message "MPY:soft reboot" coming up on screen. Every time I run my raspberry pi pico I get this message, can you tell me what it means and how to stop it happening. thanks Alistair
I am trying so hard to get this figured out, I can't seem to get my program to run on the second core...
Where in the documentation did you found that multithreading is implemented using multicore ? The MicroPython documentation regarding threads is quite minimalist and point to the CPython page, which does not speak about cores... Beside MicroPython also run on single core platform... Thanks
@warped-sliderule
Жыл бұрын
Very good question! If it based on CPython, then CPython uses GIL (Global Interpreter Lock), and threads do NOT run concurrently across cores. Python on Win and Linux use GIL, do not run concurrently even thought they clearly have multiple cores. I doubt that Micropython would tackle concurrent threads when big Python has not, but who knows. To test, create multiple threads and have only one thread halt, or enter infinite loop, but not exit, after a period of time running. If all the threads stop, then GIL is preventing concurrent (multi-core) threads.
I understand the Pico treats can run a thread on each core.. but the ESP32 running its 16 threads on both cores or just the one? 8 each? Under the hood, I assume each core has it's own dedicated area of flash? Or is are the 2 cores accessing the same program.. sharing the same RAM? For example, is it possible to run micropython on one core, and a totally separate application on the second core?
How do you control the thread affinity?
@kevinmcaleer28
2 жыл бұрын
Hi Carlos - I’m not aware of any ability to set the affinity in MicroPython. You can probably do it in C though
Q Thanks Kevin for thé video. Don’t you think that using thread locking defeats the purpose of multitasking?. I would like to hear your opinion on this. Many thanks 👍🏻
Hi Kevin, I'm new in multithreading and so I asked myself if I need to aquire allocation lock only if I overwrite a variable like you did or even if I read it. I want to use core0 on my Pico to handle some interrupts and capture timestamps with these. The duty of core1 would be to calculate the difference between two timestamps and send the data to a display (and refresh the display and all those slow stuff...). But if I have to aquire allocation lock every time thread 2 wants to read a timestamp then I think I would have no benefit in using both threads, am I wrong? Please excuse if the question is may a bit stupid or newbie but is multithreading the Pico generally a good idea for getting a higher resolution on time measuring. Greetings :)
@michaelwershofen8950
2 жыл бұрын
Got dual-threading to work now. Don't know yet if it's really faster now but hey, I did it! But be careful the pico seems to have some trouble with it's automatic garbage collector when both threads are used. I have to call gc.collect() on both threads about every 100ms to prevent the pico from freezing. Didn't have such problems when using it in single thread mode with comparable code.
I had a quick try, with two function, one blinks the onboard LED, other print out accumulated 1 number every second. it looks like the onboard led can't be opened in thread. however, if I open the print function in thread, i can then sort of "normal" blink led asynchronisingly. import machine import _thread import time def led(): led = machine.Pin('LED', machine.Pin.OUT) while True: led.on() time.sleep(.5) led.off() time.sleep(.5) def pnt(): i=0 while True: i += 1 print(f" This is the {i} cycle......", sep="", end="") time.sleep(1) if __name__ == "__main__": _thread.start_new_thread(pnt, ()) # _thread.start_new_thread(led, ()) led()
Another good video. Could you do the same in Arduino C++ ?
@kyuchumimo
2 жыл бұрын
No. Arduino boards are usually single core and they use another programming language different from MicroPython
@32_bits
2 жыл бұрын
@@kyuchumimo Explaining better. Using the same dual core 2040 but in C++ (Arduino) instead of Python to use both cores. Maybe there isn't a C libuary for this or no 20240 dual core support for this MCU in Arduino, but would be so much better to have the option in C++. BTW dual core support in Arduino C++ is already available for the ESP32.
Hi, Kevin, you have good videos and content, but if you could make it little faster and shorter 15min, you may get more viewers
@meffspa1465
2 жыл бұрын
But then what is more important, the shallow value of "viewers", or the actual worth of the content? Of course, optimally you'd have both.
Philosophers just need to learn to eat with their hands like the rest of the unsophisticated ..... problem solved, without that much brain work