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

  • P

    @Anudeep Thanks a lot m8, I actually made a very silly mistake of including the https:// while entering FIREBASE_HOST, where I must have entered the Domain name alone and removed the https:// extension.

    Thanks again for replying that quick. Cheers.

    read more
  • A

    Hello @Palani, In the Firebase, there are two types of storage,

    Cloud Firestore (Very recent one & NoSQL database) Realtime Database (To which we upload the data using NodeMCU)

    In the Arduino Code, you can see that there is a variable named FIREBASE_AUTH.

    This variable should be assigned to the Database secret. This you can find in Project Settings -> Service accounts -> Database Secrets.

    My mistake is, i have mistakenly assigned thay to Web API key. This is found at Project Settings -> General.

    The error can also occur when u don't initialise a proper realtime database in Firebase. (This is not my case).

    read more
  • P

    @Anudeep Hello Anudeep could you please help me out by elaborating the fix to get rid of that error?
    Thanks in advance.

    read more
  • At Adobe, weโ€™re focused on bringing together the most intelligent, driven, diverse set of individuals and give them the freedom to nurture their intellectual curiosity. We provide them with the necessary resources and support to shape their ideas into tangible results.
    Through the Adobe India Women-in-Technology Scholarship, we are striving towards creating gender equality in science, technology and engineering domains by encouraging women to showcase their excellence in computing and technology and become future leaders and role models in the field.

    ๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฎ๐—ฝ๐—ฝ๐—น๐˜†?

    Applications must be submitted through their dedicated application portal which opens on August 20th, 2020 and will close on September 20th, 2020, 5 pm India time.

    ๐—”๐—ฝ๐—ฝ๐—น๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฑ๐—ฒ๐—ฎ๐—ฑ๐—น๐—ถ๐—ป๐—ฒ:

    Application deadline: September 20th, 2020, 5 pm India time.

    Visit:- https://research.adobe.com/adobe-india-women-in-technology-scholarship/

    read more
  • @kowshik1729 If you are using an external high-speed switch, there is only one one clock generator for both wifi and BLE and the high-speed switch you mentioned is Ultra-Wideband (UWB) 3โ€“8 GHz from its datasheet

    b2da6871-9110-40f1-8ff0-d8313547a17a-image.png

    you also check this solution that I shared in the previous post.

    @salmanfaris said in BT and WiFi Dual Core simultaneous operation using ESP32?:

    There is a special option called โ€œSoftware controls WiFi/ Bluetooth coexistenceโ€ it can control the ESP32's Bluetooth and Wi-Fi coexistence using software, more details: https://www.espressif.com/sites/default/files/documentation/ESP32_FAQs__EN.pdf

    read more