Micropython Threads - Use Both Cores, on Raspberry Pi Pico and ESP32

  Рет қаралды 22,840

Kevin McAleer

Kevin McAleer

2 жыл бұрын

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.com/membership
Join the list - action.smarsfan.com/join-the-...
☕️ Enjoy this video? Buy me a coffee! www.buymeacoffee.com/kevinmca...
📸 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/kevinmcaleer/t...
🎵 Music by Epidemic Sounds www.epidemicsound.com/referra...
#Pico​ #MicroPython​ #Threads

Пікірлер: 52
@christianjensen1846
@christianjensen1846 Жыл бұрын
sincerely thank you for making this video. your whole channel has taught me so much i needed to know
@kevinmcaleer28
@kevinmcaleer28 Жыл бұрын
Thanks Christian!
@skyagnitti
@skyagnitti 3 ай бұрын
same here mate, this was EXACTLY what I was looking for to learn how to do multi-threading for my esp32 and rPiW microcontrollers :)
@aporfirio
@aporfirio Ай бұрын
Thank you man, is what I was looking for !!!!!. Excelente !!!!
@martincregg
@martincregg Жыл бұрын
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.
@christopherlyons7613
@christopherlyons7613 Жыл бұрын
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.
@KaushikYogesh
@KaushikYogesh 2 жыл бұрын
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
@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.
@himdimzma
@himdimzma 2 жыл бұрын
Really good presentation and explanation of threads. Thanks
@aldoercigoj8769
@aldoercigoj8769 Жыл бұрын
You really are the best explaining this topic, thank you very much for sharing the knowledge. Greetings from Argentina.
@kevinmcaleer28
@kevinmcaleer28 Жыл бұрын
Thanks Aldo!
@sermadreda399
@sermadreda399 2 жыл бұрын
Great video ,thank you for sharing
@MrToy007
@MrToy007 Жыл бұрын
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 :)
@stephencallaghan2150
@stephencallaghan2150 2 жыл бұрын
Thank you for sharing your knowledge with us. It gives me a great place to start with multi-threading.
@kevinmcaleer28
@kevinmcaleer28 2 жыл бұрын
Thanks Stephen!
@derekanderson7076
@derekanderson7076 2 жыл бұрын
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
@mattgiles6171 Жыл бұрын
This is what I thought!
@DavidCousins
@DavidCousins Жыл бұрын
Thanks for the video
@Kris-cd9qs
@Kris-cd9qs 8 ай бұрын
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 ;)
@alexgathua1736
@alexgathua1736 2 жыл бұрын
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
@kevinmcaleer28 2 жыл бұрын
Thanks Alex! Glad they are useful
@terjidjurhuus1917
@terjidjurhuus1917 2 жыл бұрын
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
@kevinmcaleer28 2 жыл бұрын
Thanks Terji!
@renobodyrenobody
@renobodyrenobody Жыл бұрын
Thanks a lot, I was able to have a website (phew) managing a neopixel ring... Thank you thank you than you!
@kraemrz
@kraemrz 2 жыл бұрын
Great video
@kevinmcaleer28
@kevinmcaleer28 2 жыл бұрын
Thanks!
@hanshaerdtle4682
@hanshaerdtle4682 10 ай бұрын
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.
@neversinkmakes
@neversinkmakes 2 жыл бұрын
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
@kevinmcaleer28 2 жыл бұрын
Great suggestion I'll give that a try next time I've got an ESP32 connected.
@hanshaerdtle4682
@hanshaerdtle4682 10 ай бұрын
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
@jorgee7396
@jorgee7396 Жыл бұрын
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.
@Atlantis1986
@Atlantis1986 Жыл бұрын
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.
@christopherlyons7613
@christopherlyons7613 7 ай бұрын
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.
@alistaircook1997
@alistaircook1997 11 ай бұрын
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
@michaelwershofen8950
@michaelwershofen8950 2 жыл бұрын
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
@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.
@bennguyen1313
@bennguyen1313 Жыл бұрын
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?
@Juan123qwerty
@Juan123qwerty Жыл бұрын
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++
@user-um3ui1gu9t
@user-um3ui1gu9t Жыл бұрын
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
@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.
@aysershuhaib478
@aysershuhaib478 Жыл бұрын
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 👍🏻
@CarlosTangerino
@CarlosTangerino 2 жыл бұрын
How do you control the thread affinity?
@kevinmcaleer28
@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
@ukissrulez
@ukissrulez Жыл бұрын
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++
@Alltechfab406
@Alltechfab406 Жыл бұрын
I am trying so hard to get this figured out, I can't seem to get my program to run on the second core...
@32_bits
@32_bits 2 жыл бұрын
Another good video. Could you do the same in Arduino C++ ?
@kyuchumimo
@kyuchumimo 2 жыл бұрын
No. Arduino boards are usually single core and they use another programming language different from MicroPython
@32_bits
@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.
@zhouyu9971
@zhouyu9971 Жыл бұрын
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()
@vjreviewsandiy
@vjreviewsandiy 2 жыл бұрын
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
@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.
@mandelbro777
@mandelbro777 Жыл бұрын
Philosophers just need to learn to eat with their hands like the rest of the unsophisticated ..... problem solved, without that much brain work
Raspberry Pi 400 Setup for Robotics - How I setup my RPI400
5:20
Kevin McAleer
Рет қаралды 1,7 М.
WHY IS A CAR MORE EXPENSIVE THAN A GIRL?
00:37
Levsob
Рет қаралды 20 МЛН
Заметили?
00:11
Double Bubble
Рет қаралды 3,5 МЛН
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 920 М.
The Petabyte Pi Project
22:27
Jeff Geerling
Рет қаралды 2,2 МЛН
Getting Started with Multicore Programming on the Raspberry Pi Pico
11:14
Learn Embedded Systems
Рет қаралды 43 М.
Pico W Episode 1: Setting Up the Raspberry Pi Pico W
12:48
Lori Pfahler
Рет қаралды 13 М.
Raspberry Pi Pico
16:16
ExplainingComputers
Рет қаралды 302 М.
Moving from Arduino to MicroPython - 10 Things you need to know.
15:49
When Did Raspberry Pi become the villain?
21:54
Jeff Geerling
Рет қаралды 1,4 МЛН
ESP32 Guide 2024 | Choosing and Using an ESP32 Board
41:06
DroneBot Workshop
Рет қаралды 231 М.
Watermelon Cat?! 🙀 #cat #cute #kitten
0:56
Stocat
Рет қаралды 22 МЛН
接下来就是路飞救两个小孩#海贼王  #路飞
0:39
路飞与唐舞桐
Рет қаралды 3,1 МЛН