• Categories
  • Recent
  • Popular
  • Tags
  • Users
  • Groups
  • Register
  • Login
MakerGram Logo

MakerGram

  • Register
  • Login
  • Search
  • Categories
  • Recent
  • Popular
  • Tags
  • Users
  • Groups

Multi Thread Handling for normal Processes using python

Raspberry Pi
3
7
8.6k
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kowshik1729
    kowshik1729 last edited by 19 Sept 2019, 14:22

    Hello all,
    I would like to know whether we can handle two different processes using python? like how our usual OS's do. For instance, I am using raspberry pi for reading video frames and at the same instance I'm handling a function where I am playing audio on speaker. Can I do both the things at the same time using two different individual threads on raspberry pi?

    If yes, please suggest me how can I achieve this. Thanks in advance..!!

    1 Reply Last reply Reply Quote 0
    • J
      jerinisready last edited by jerinisready 20 Sept 2019, 04:49 20 Sept 2019, 04:38

      I suggest you multiprocessing to handle dlifferent processes.
      Use multiprocessing.Process.
      If you want to establish a communication between parent and child process, use multiprocessing.Queue or multiprocessing.Pipe

      1 Reply Last reply Reply Quote 0
      • kowshik1729
        kowshik1729 last edited by 20 Sept 2019, 11:53

        @jerinisready Thank you soo much. Is Multiprocessing.process is an python library? Can i get it through PIP installer?

        J 1 Reply Last reply 20 Sept 2019, 12:38 Reply Quote 0
        • salmanfaris
          salmanfaris last edited by 20 Sept 2019, 12:35

          Hi @kowshik1729 ,

          Yes, you can do Multi Thread in Raspberry Pi using python and note that different threads do not actually execute at the same time: they merely appear to.

          We can easily implement simple threads using threading module. I'll show you some example of how actually it works.

          Example :
          I was trying to blink two LED's at the same time, for that I created a function that does the blink infinitely using while(True) .

          import RPi.GPIO as GPIO
          import time
          
          Rled = 12
          Bled = 6
          
          GPIO.setmode(GPIO.BCM)
          
          GPIO.setup(Rled,GPIO.OUT)
          GPIO.setup(Bled,GPIO.OUT)
          
          
          def blue():
                  while True:
                          print "LED BLUE is  ON"
                          GPIO.output(Bled,GPIO.LOW)
                          time.sleep(1)
                          print "LED BLUE is  OFF"
                          GPIO.output(Bled,GPIO.HIGH)
                          time.sleep(1)
          def red():
                  while True:
                          print "LED RED is  ON"
                          GPIO.output(Rled,GPIO.LOW)
                          time.sleep(1)
                          print "LED RED is OFF"
                          GPIO.output(Rled,GPIO.HIGH)
                          time.sleep(1)
          
          blue()
          red()
          
          

          Output :

          Capture.PNG

          Video : https://www.youtube.com/watch?v=AypMEQFpEWo&feature=youtu.be

          since we are using the infinity loop for both methods, the blue() will not stop and red() will not invoke either.

          In these scenarios, we can use multithread both function without waiting for another , let's try.

          import RPi.GPIO as GPIO
          import time
          import threading
          
          
          
          Rled = 12
          Bled = 6
          
          GPIO.setmode(GPIO.BCM)
          
          GPIO.setup(Rled,GPIO.OUT)
          GPIO.setup(Bled,GPIO.OUT)
          
          
          def blue():
                  while True:
                          print "LED BLUE is  ON"
                          GPIO.output(Bled,GPIO.LOW)
                          time.sleep(1)
                          print "LED BLUE is  OFF"
                          GPIO.output(Bled,GPIO.HIGH)
                          time.sleep(1)
          def red():
                  while True:
                          print "LED RED is  ON"
                          GPIO.output(Rled,GPIO.LOW)
                          time.sleep(1)
                          print "LED RED is OFF"
                          GPIO.output(Rled,GPIO.HIGH)
                          time.sleep(1)
          
          
          t1 = threading.Thread(target=blue)
          t2 = threading.Thread(target=red)
          
          
          t1.start()
          t2.start()
          
          

          Output :

          Capture2.PNG

          Video : https://www.youtube.com/watch?v=HMKYzgdNwd8&feature=youtu.be

          with the help threading module we can simply run both methods at the same time (how we feel).

          I think you got your answer. also, keep in mind to avoid race condition and deadlock while multithreading.

          1 Reply Last reply Reply Quote 2
          • J
            jerinisready @kowshik1729 last edited by jerinisready 20 Sept 2019, 12:43 20 Sept 2019, 12:38

            @kowshik1729
            that's builtin at python. only thing is. you can simply import using

            from multiprocessing import Process
            def my_function_to_run(*args, **kwargs):
                 ...
                 ...
            
            def main():
                p1 = Process(
                               target=my_function_to_run, 
                               args=('arg_01', 'arg_02', 'arg_03', ), 
                               kwargs={'key': 'value', 'another_key':True}
                )
                p1.start()
                p2 = Process(
                               target=my_function_to_run, 
                               args=('arg_01', 'arg_02', 'arg_03', ), 
                               kwargs={'key': 'value', 'another_key':True}       
                )
                p2.start()
                
                p1.join()
                p2.join()
                print("Finished!")
            
            if __name__ == '__main__':
               main()
            

            This Might can help: https://docs.python.org/2/library/multiprocessing.html

            1 Reply Last reply Reply Quote 1
            • kowshik1729
              kowshik1729 last edited by 20 Sept 2019, 12:52

              @salmanfaris Thankyou soo much for awesome example. The video really helped me what it looks like without multi threading. Thank you.

              salmanfaris 1 Reply Last reply 20 Sept 2019, 14:40 Reply Quote 1
              • salmanfaris
                salmanfaris @kowshik1729 last edited by salmanfaris 21 Sept 2019, 08:05 20 Sept 2019, 14:40

                @kowshik1729 happy hacking and keep making.
                all the best 😊 👍

                1 Reply Last reply Reply Quote 0
                1 out of 7
                • First post
                  1/7
                  Last post

                Recent Posts

                • Hi @borncurious , For the industrial purpose you can go with Raspberry Pi compute modules, Take a look at the latest version CM4 - https://www.raspberrypi.com/products/compute-module-4/ , Which is designed for industrial usage and incorporates a quad-core ARM Cortex-A72 processor, dual video output, and a wide selection of other interfaces. Available in 32 variants, with a range of RAM and eMMC Flash options, and with or without wireless connectivity.

                  Other options are, boards like RockPi and Orange Pi boards which are similar to Raspberry similar model and specs but the price is a bit lower than the raspberry pi. Make sure to check the Interface and driver support that you need to incorporate into your project since these boards are not documented and maintained like the official raspberry pi as far as I know.

                  also, you can check Toradex System on Module.

                  12 Jun 2023, 05:32 • read more
                • B

                  Is raspi generally used in commercial product/end product.if not can u guys suggest raspi alternatives for end products.

                  7 Jun 2023, 06:50 • read more
                • https://www.youtube.com/watch?v=1EIFfln3Kxs&t=1s

                  9 Jan 2023, 19:48 • read more
                • 1666287f-258d-437d-b2c5-91851d08617c-image.png
                  The classic compact camera for Raspberry Pi with a 12MP sensor and autofocus

                  Available from $25 with your choice of standard and wide lenses, with or without an infrared filter

                  bf34763e-5ba3-4962-8e92-75583fd1e96c-image.png

                  More details: https://www.raspberrypi.com/products/camera-module-3/

                  https://youtu.be/XwYRSlqCN3A

                  Resource:
                  Documentation: https://www.raspberrypi.com/documentation/accessories/camera.html
                  Camera Software: https://www.raspberrypi.com/documentation/computers/camera_software.html

                  9 Jan 2023, 19:46 • read more
                • Hey @rafitc99 ,

                  Did your issue resolve? Curious to know 😊

                  1 Nov 2022, 08:17 • read more
                By MakerGram | A XiStart Initiative | Built with ♥ NodeBB
                Copyright © 2023 MakerGram, All rights reserved.
                Privacy Policy | Terms & Conditions | Disclaimer | Code of Conduct