Multi Thread Coding on the Raspberry Pi Pico in MicroPython - Threads, Locks and problems!

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

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

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

    It would be interesting to compare the maximum frame rates of each method to determine how much more efficient each was compared to using only one core. There must be some overhead involved in switching cores that could be quite significant.

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

      Hi. I did do some work on that in making this little RP2040 console - th-cam.com/users/shortsQusjn49OLL4.
      I do plan a video based around it but work has been quite hectic for the past few months.
      The core switching isn't such an issue. Once you get your threads up and running they really do work in parallel. The key is in dividing up the tasks and making sure that one doesn't block the other when accessing resources etc.

  • @x_CrossHair_x
    @x_CrossHair_x 2 หลายเดือนก่อน

    I'm Finally getting the time to play with many of these Microcoller Modules "Pi's / Arduino's / ESP32's".. I appreciate all the Tips and Tricks Videos like these.. (For Newbies like ME) 👍👍😊

    • @BytesNBits
      @BytesNBits  2 หลายเดือนก่อน

      Glad you like them!

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

    Liked your tutorial, with a stream of examples from understanding the basics to a real case usage.
    I was searching threading related info for Raspberry Pi Pico, and this tutorial nicely appeared: I'm sure I'll give it a try.
    Thank you very much. Andrea

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

      Great. Definitely put multi core into your programming toolbox.

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

    Really useful, going to use it to control two independent motors, one steering, one driving... Nice

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

      Glad it helped

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

    Excellent explanation and great video. Thank you for helping us to better understand threading on Python.

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

      Hi. No problem. I hope you're found it useful!

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

    Have followed your tutorials with great interest; excellent work, very well explained. Thank you very much for sharing your insights. Rob

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Great to hear you're enjoying them.

  • @FranciscoJavier32423
    @FranciscoJavier32423 5 หลายเดือนก่อน

    Thanks a lot, really good explanation. My automatic plants watering system soon will be working

    • @BytesNBits
      @BytesNBits  4 หลายเดือนก่อน +1

      Glad it helped

  • @sambeard4428
    @sambeard4428 2 ปีที่แล้ว

    Great thorough explanation, thanks for that. One point of critique, I'm not a big fan of the 'live drawing' animation style found at the beginning of the video. It adds a lot of visual noise and especially distracts from your speaking. This however is a personal preference but imo worth mentioning

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

      Glad you enjoyed the video. Sorry the whiteboard animations are not your cup of tea.

  • @mandelbro777
    @mandelbro777 2 ปีที่แล้ว

    Great explanation for beginners. Really loved the video.

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Glad you liked it

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

    I’m not sure about the fact that the second thread is running on the second core. I’ve already used threads in my projects but with an ESP32-C3, so a single core microcontroller and it runs without problems.

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

      The Pico handles threads differently to the ESP32 in MicroPython. On the ESP32 MicroPython code can only run on one of the cores so your threads are running as tasks on the same core. The Pico allows you to run MicroPython on both cores (mostly) but can also use the asyncio package to get multiple tasks per core.

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

    THANK YOU SIR FOR THIS DETAILED EXPLANATION :))))))))))))))
    Greetings from Morocco

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

      Glad it was helpful!

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

    You're awesome. Thank you for what you do

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      You're very welcome.

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

    Thank you for the time and insight you provide with your channel.
    If each thread waits for the other one to finish its access to the frame buffer, what is the advantage of using two cores? The renderer can only do a little work (some Trig maybe) before its main job of accessing the frame buffer. The two cores would indeed run in parallel if the frame buffer were double-buffered, but of course this is not possible with the small Pico RAM size.

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

      You're right about the double buffering but this setup can still work well. The idea here is that you split your code into an update phase and a render phase. The update phase is the parallel part so you try to put as much of the processing load into that. The bouncing box demo doesn't really have any update processing but the Asteroids game (I hope that's shown in the video!) performs all the polygon rotations, collision detection, etc. leaving only the actual drawing of lines into the buffer for the render phase. You can actually make some great multi thread savings this way.

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

      @@BytesNBits Great answer, thanks.

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

    then theres the PIO stuff. they basically function as 'event computers' that generate CPU interrupts based on the interaction between a state machine and the input stream. This makes it easy for drivers to be efficient because there is no impedance gap to cross converting from a raw data stream to contextually meaningful IO events. While the PIO is figuring out what events it should tell the CPU about happening on the IO pins, the CPU is free to do whatever it likes, and is not occupied doing this work itself like it would be in many other computing environments.

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

      Thanks for the info. PIO is something I intend to look at in some upcoming videos.

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

    This was on point! Thanks so much. Worth every second of it!!

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Glad it was helpful!

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

      @@BytesNBits today I was able to output some sound with a buzzer and some animation in the oled screen both at the same time thanks to your video. Im not sure if there is other way to do it but at least is working with threads.

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      @@1Geek4Ever1 Great! There are a number of ways to solve any issue. Go with the one that works for you.

  • @L2.Lagrange
    @L2.Lagrange 7 หลายเดือนก่อน

    Very useful tutorial.

    • @BytesNBits
      @BytesNBits  7 หลายเดือนก่อน

      Glad it was helpful!

  • @bobtntcnl
    @bobtntcnl 2 หลายเดือนก่อน

    thanks! what micropython fw version do you use? so main question is does _thread module can be used in production code?

    • @BytesNBits
      @BytesNBits  2 หลายเดือนก่อน

      If you check the Micropython docs it's still listed as experimental - docs.micropython.org/en/latest/library/_thread.html.
      I'd be wary of using it in anything where a system crash would be bad.

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

    a 40 min vid,let me get the snacks!

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

      Great. Hope you enjoy it! Some popcorn and a coke for me!

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

    great explanation. tnx.

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

      Glad it was helpful!

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

    Thanks for the great video. 👍

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

      Thanks for watching!

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

    I was having the same crashes where my rpi got frozen using the second core... The way I fixed it was using a "return 0" at the end of the function instead of the GC.

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

      I hadn't considered that. I'll give it a go. Thanks for the tip.

  • @billyheng4824
    @billyheng4824 2 ปีที่แล้ว

    Will there be any issue when apply the same method to all the other cores available ? Assumming earch cores handle one box redenering around the LCD and running around. The use case is appilcable for max. 8 application running concuerrently or different input running curently ?

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Hi. The RP2040 only has 2 cores for you to use. You can only run 1 thread per core. If you are using a processor with more cores then the same method would apply.

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

    Great video

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

      Glad you enjoyed it

  • @HitAndMissLab
    @HitAndMissLab 2 ปีที่แล้ว

    What is that IDE that you are using?
    As well could this MicroPython setup you are using guarantee timing precision during execution?

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Hi. I use PyCharm for my coding. There is a free community version that does everything you need. Make sure you install the MicroPython plugin so it can talk to the Pico.
      MicroPython is an interpreted language so is much slower than C++. It is aware of time and you can use this to make sure things happen at the correct time. If you are trying to get very tight timing e.g. some sort of signal timing, then I'd probably go with C, or have a play with the PIO system.

  • @awesomegamer31
    @awesomegamer31 2 ปีที่แล้ว

    Wondering if you have any more info on how to use the REPL tool in PyCharm - for example, flashing a program to it causes it to run immediately, but when opening REPL the keyboard interrupt ends it. Using the soft reboot is enough to reset it, but it's an issue using the socket library for my web sever as I get the EADDRINUSE OSError, essentially that the static IP address I want to bind to is already being used, but by the instance that was interrupted.
    You seem to be able to just upload and start it in a few examples here? Plus the terminal window you use just says 'Local', mine has a path to "...\venv\Scripts\python.exe". And obviously I can't flash a new version of the file unless I close the instance of REPL terminal as the COM port is in use...
    If you know of any sources online about how to use REPL to debug beyond the official documentation (which is not great) I would appreciate it a ton :)

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Hi. Make sure you're using the latest version of PyCharm. They fixed a number of REPL bugs a few months ago. After that make sure you've got the project set up and the extra Micropython add ons installed. If you upload a file called main.py it will auto execute, or if one already exists that will execute. Try saving the main entry point for your code as something different , e.g mycode.py, and delete any main.py. Once you upload you'll be able to REPL onto the device and start the code manually, import mycode.

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

    i am trying to program a clock (micropython, raspberry pi pico). my goal is to be able to set the time while the clock is ticking. is this possible or is it a contradiction.

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

      That should be fine. Your code will need to take account of the time reset in the way it tracks time. I'll leave that to you to work out!

  • @graydrake6855
    @graydrake6855 11 หลายเดือนก่อน

    even if makes the example understandable, you should mention that it's never a good idea to do a sleep() while you hold a lock, which another thread may be waiting on

    • @BytesNBits
      @BytesNBits  11 หลายเดือนก่อน

      Thanks for the tip.

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

    FreeRTOS and Multicore.

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Thanks for the tip. I haven't played around with RTOS yet.

  • @V1P3RSlab
    @V1P3RSlab 2 ปีที่แล้ว

    Hello, wanna try your example, but only got a 1.8 tft spi (128x260) (the red one with sd card reader) and no MISO nor MISI but only A0 and SDA ... how can i adapt teh code?

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

      Hi. Are you sure this is an SPI lcd? Those connections sound more like part of an I2C interface.

    • @V1P3RSlab
      @V1P3RSlab 2 ปีที่แล้ว

      @@BytesNBits they sell like "AZDelivery Display SPI TFT 128 x 160 Pixel" :-(

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

      @@V1P3RSlab I think A0 is the DC pin. SDA is MOSI. There is no MISO as this isn't really needed for the TFT panel. The other pins should match the video. I hope this helps.

    • @V1P3RSlab
      @V1P3RSlab 2 ปีที่แล้ว

      @@BytesNBits trying, i let you know :D

    • @V1P3RSlab
      @V1P3RSlab 2 ปีที่แล้ว

      @@BytesNBits no way. Using pycharm i got some compile errors on library versions, using thonny, i adapt the code but receive the "unsupported display"

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

    Thank you!

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

      Hope you're finding these useful.

  • @samneggs1
    @samneggs1 2 ปีที่แล้ว

    _thread has been worked on since the official 1.18 version. Try using a recent nightly build, it appears much more robust.

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Hi. Thanks for the tip. I'll give that a go.

  • @jyvben1520
    @jyvben1520 2 ปีที่แล้ว

    Is the buffer a string of 75KB ? are 2 buffers used ?

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Hi. Basically yes - 75KB of single byte integers / characters. There's not enough memory for 2 buffers so the processes have to share the one memory block. Double buffering would be great but not really an option without more RAM.

    • @jyvben1520
      @jyvben1520 2 ปีที่แล้ว

      @@BytesNBits how many colours are used ? could 4 bits be enough ?

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      @@jyvben1520 Hi. For the LCD panel I was using it's 16bits per pixel. You can store the colour info in memory in any way you want but you have to expand it out to the full 16 bits to send to the LCD. Pure Python isn't fast enough to do this on the Pico.

  • @Новости-з3й
    @Новости-з3й 2 ปีที่แล้ว +1

    Where did you got the idea that multithreading is implemented using the 2 cores ?
    There is nothing in the documentation that can lead to this conclusion
    Please explain why you think so. Also explain what happens if you declare 3 threads...

    • @BytesNBits
      @BytesNBits  2 ปีที่แล้ว

      Hi. The Micropython threading package assumes one thread per core. I know in more powerful processors you can run multiple threads per cores but that doesn't seem to be implimented in MicroPython.

    • @Новости-з3й
      @Новости-з3й 2 ปีที่แล้ว

      @@BytesNBits Again, there is no such thing in the documentation
      The Micropython documentation points to CPython documentation and CPython does not know about multicore.
      Beside, MicroPython runs also on the ESP32 which has only one core...
      Please post the web page adress where you found this info
      Thanks

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

      @@Новости-з3й Thanks for your comments and tips.

    • @Новости-з3й
      @Новости-з3й 2 ปีที่แล้ว

      @@BytesNBits Finaly, I checked the core assumption using a simple script which reads the CPUID register from the RP2040 and you're right, the first thread runs on core 1
      Adding a second thread as you mentioned leads to an error
      Here is the script for those who are interested :
      -----------------------------------------------------------
      import time, _thread
      from machine import mem32,Pin
      led = machine.Pin(25, machine.Pin.OUT)
      def Core0():
      while True:
      led.on()
      time.sleep(5)
      #read CPUID
      print("Core0 = ",mem32[0xd0000000])

      def Core1():
      while True:
      led.off()
      time.sleep(5)
      #read CPUID
      print("Core1 = ",mem32[0xd0000000])
      _thread.start_new_thread(Core1, ( ))
      time.sleep(1)
      Core0()
      -----------------------------------------------
      Core0 output is 0 and Core1 output is 1
      Regards

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

    No thanks. I’ve no tolerance for garbage collection.

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

      The package is still in development. Hopefully this is just a work around until the bugs get ironed out.

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

    Python 🤦

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

      MicroPython is the microcontroller version of Python. Pretty much the same.

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

    Lol ... Couldn't get a harder way to complicate a basic example??? 😂😂😂😂