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

  Рет қаралды 24,229

Kevin McAleer

Kevin McAleer

Күн бұрын

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
@christianjensen1846
@christianjensen1846 2 жыл бұрын
sincerely thank you for making this video. your whole channel has taught me so much i needed to know
@kevinmcaleer28
@kevinmcaleer28 2 жыл бұрын
Thanks Christian!
@skyagnitti
@skyagnitti 8 ай бұрын
same here mate, this was EXACTLY what I was looking for to learn how to do multi-threading for my esp32 and rPiW microcontrollers :)
@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.
@martincregg
@martincregg 2 жыл бұрын
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.
@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 2 жыл бұрын
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.
@aporfirio
@aporfirio 6 ай бұрын
Thank you man, is what I was looking for !!!!!. Excelente !!!!
@aldoercigoj8769
@aldoercigoj8769 2 жыл бұрын
You really are the best explaining this topic, thank you very much for sharing the knowledge. Greetings from Argentina.
@kevinmcaleer28
@kevinmcaleer28 2 жыл бұрын
Thanks Aldo!
@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 2 жыл бұрын
This is what I thought!
@himdimzma
@himdimzma 2 жыл бұрын
Really good presentation and explanation of threads. Thanks
@hanshaerdtle4682
@hanshaerdtle4682 Жыл бұрын
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.
@hanshaerdtle4682
@hanshaerdtle4682 Жыл бұрын
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
@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!
@Kris-cd9qs
@Kris-cd9qs Жыл бұрын
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
@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 :)
@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.
@sermadreda399
@sermadreda399 2 жыл бұрын
Great video ,thank you for sharing
@DavidCousins
@DavidCousins 2 жыл бұрын
Thanks for the video
@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!
@k98killer
@k98killer 2 ай бұрын
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.
@m.preacher2829
@m.preacher2829 3 ай бұрын
how about Raspberry Pi zero 2w ? Can it runs more threads??
@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.
@Новости-з3й
@Новости-з3й 2 жыл бұрын
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.
@alistaircook1997
@alistaircook1997 Жыл бұрын
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
@bennguyen1313
@bennguyen1313 2 жыл бұрын
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++
@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.
@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.
@christopherlyons7613
@christopherlyons7613 11 ай бұрын
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.
@renobodyrenobody
@renobodyrenobody Жыл бұрын
Thanks a lot, I was able to have a website (phew) managing a neopixel ring... Thank you thank you than you!
@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 👍🏻
@kraemrz
@kraemrz 2 жыл бұрын
Great video
@kevinmcaleer28
@kevinmcaleer28 2 жыл бұрын
Thanks!
@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++
@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
@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...
@zhouyu9971
@zhouyu9971 2 жыл бұрын
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()
@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.
@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 2 жыл бұрын
Philosophers just need to learn to eat with their hands like the rest of the unsophisticated ..... problem solved, without that much brain work
What is PicoZero?
8:03
Kevin McAleer
Рет қаралды 5 М.
1 сквиш тебе или 2 другому? 😌 #шортс #виола
00:36
Всё пошло не по плану 😮
00:36
Miracle
Рет қаралды 1,8 МЛН
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 39 МЛН
Raspberry Pi Pico Stepper Motors via PIO
25:17
Tinker Tech Trove
Рет қаралды 61 М.
Getting Started with Multicore Programming on the Raspberry Pi Pico
11:14
Learn Embedded Systems
Рет қаралды 45 М.
Every Developer Needs a Raspberry Pi
27:27
Sam Meech-Ward
Рет қаралды 743 М.
I tried the 3 Cheapest Arduino Alternatives! (That you Suggested)
13:21
How to use Timers on the RPi PICO
17:05
Making Stuff with Chris DeHut
Рет қаралды 16 М.
Raspberry Pi Pico
16:16
ExplainingComputers
Рет қаралды 313 М.
Writing fast and efficient MicroPython
31:42
PyCon AU
Рет қаралды 69 М.
PicoMite: Running BASIC on a Raspberry Pi Pico
19:29
ExplainingComputers
Рет қаралды 147 М.
1 сквиш тебе или 2 другому? 😌 #шортс #виола
00:36