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

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 พ.ย. 2024

ความคิดเห็น • 54

  • @christianjensen1846
    @christianjensen1846 2 ปีที่แล้ว +4

    sincerely thank you for making this video. your whole channel has taught me so much i needed to know

    • @kevinmcaleer28
      @kevinmcaleer28  2 ปีที่แล้ว +1

      Thanks Christian!

    • @skyagnitti
      @skyagnitti 8 หลายเดือนก่อน +1

      same here mate, this was EXACTLY what I was looking for to learn how to do multi-threading for my esp32 and rPiW microcontrollers :)

  • @martincregg
    @martincregg 2 ปีที่แล้ว +3

    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 ปีที่แล้ว +1

    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 ปีที่แล้ว +3

    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.

  • @derekanderson7076
    @derekanderson7076 2 ปีที่แล้ว +3

    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.

  • @aldoercigoj8769
    @aldoercigoj8769 2 ปีที่แล้ว +1

    You really are the best explaining this topic, thank you very much for sharing the knowledge.
    Greetings from Argentina.

  • @himdimzma
    @himdimzma 2 ปีที่แล้ว +1

    Really good presentation and explanation of threads. Thanks

  • @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 ปีที่แล้ว +3

    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

  • @stephencallaghan2150
    @stephencallaghan2150 2 ปีที่แล้ว +1

    Thank you for sharing your knowledge with us. It gives me a great place to start with multi-threading.

  • @aporfirio
    @aporfirio 6 หลายเดือนก่อน +1

    Thank you man, is what I was looking for !!!!!. Excelente !!!!

  • @k98killer
    @k98killer 3 หลายเดือนก่อน

    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.

  • @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

  • @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 :)

  • @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.

  • @DavidCousins
    @DavidCousins 2 ปีที่แล้ว +1

    Thanks for the video

  • @sermadreda399
    @sermadreda399 2 ปีที่แล้ว +1

    Great video ,thank you for sharing

  • @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.

  • @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.

  • @m.preacher2829
    @m.preacher2829 3 หลายเดือนก่อน +1

    how about Raspberry Pi zero 2w ? Can it runs more threads??

  • @Новости-з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.

  • @terjidjurhuus1917
    @terjidjurhuus1917 2 ปีที่แล้ว +3

    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! ;-)

  • @neversinkmakes
    @neversinkmakes 2 ปีที่แล้ว +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 ปีที่แล้ว +1

      Great suggestion I'll give that a try next time I've got an ESP32 connected.

  • @kraemrz
    @kraemrz 2 ปีที่แล้ว +1

    Great video

  • @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

  • @christopherlyons7613
    @christopherlyons7613 ปีที่แล้ว

    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.

  • @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++

  • @renobodyrenobody
    @renobodyrenobody ปีที่แล้ว

    Thanks a lot, I was able to have a website (phew) managing a neopixel ring... Thank you thank you than you!

  • @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?

  • @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 ปีที่แล้ว +1

    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

  • @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 ปีที่แล้ว +1

      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.

  • @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...

  • @ukissrulez
    @ukissrulez ปีที่แล้ว +2

    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++

  • @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 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()

  • @vjreviewsandiy
    @vjreviewsandiy 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +2

    Philosophers just need to learn to eat with their hands like the rest of the unsophisticated ..... problem solved, without that much brain work