Multi Thread Handling for normal Processes using python



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



  • 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



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



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



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



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



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


Log in to reply
 

Recent Posts

  • S

    @arunksoman thankyou so much...your reply really busted the stress out of me...can you help with any documentations which i could possibly go through

    read more
  • A

    @sreu13 It is possible and a far far better method.

    read more
  • S

    @arunksoman hi,..
    actually at this point of time, I'm not willing to risk RPI4,....i thought of connecting the rpi4 to google cloud (already have an account with $300 credit) and proceeding with the code.
    but i do not know if the below process can be executed

    sending image from rpi4 to cloud using this cloud for running prediction the above two processes should be automated and image should be sent to cloud when pi boots up(VM engine can be activated whenever nessesary)

    is this possible?

    read more
  • A

    @sreu13 I edited comment please read that again. Most probably it will not effect your file system. In some scenarios it can create worse effect.

    read more
  • S

    @arunksoman i'll try this method, but while executing swap command, will the rasbian os and the files it contains be effected??

    read more