'Multi Threading from a function to another function and back to the main function

currently this is my setup, python in PyCharm, and an Arduino Uno with MAX30100 sensor for heart rate. I am currently able to transmit data via pyserial into my pycharm from arduino, and will show the heartrate in pycharm. But I want my program to run the webcam feed WHILE showing the sensor reading at the same time. Currently my SIMPLIFIED code is as below :

class start_workout_session():

def pushup(self):
   
    #cam start here ~~!!
    cap = cv2.VideoCapture(0)

    with mp_pose.Pose(min_detection_confidence=1.5, min_tracking_confidence=1.5) as pose:

        while cap.isOpened():
           
            # Extract landmarks
            try:
             
               calculate angle here

               Showing angle in frame by using putText method from cv2
               
               Curl counter logic

               Render curl counter

                t1 = threading.Thread(target=arduino)
                t1.start()
    
               cv2.imshow('i-Trainer', image)

        cap.release()
        cv2.destroyAllWindows()



def arduino():
serialInst = serial.Serial()
serialInst.baudrate = 9600
serialInst.port = 'COM3'
serialInst.open()
while True:
    if serialInst.inWaiting:
        packet = serialInst.readline()
        print(packet.decode('utf').rstrip('\n'))

time.sleep(1)

t2 = threading.Thread(target=pushup)
t2.start()
t1.join()
t2.join()

this program can run but it always prompts an error :

File "C:\Users\..\main.py", line 629, in arduino
    serialInst.open()



File "C:\Users\..\Python36\lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)


Solution 1:[1]

Thanks to @cguk70,

i managed to solve the problem, by putting the

 t1 = threading.Thread(target=arduino)
                t1.start()

outside together with

#cam start here ~~!!
    cap = cv2.VideoCapture(0)

reason is that i am creating a new Arduino thread inside the camera loop. So the second time round you'll try and open the serial port again and get the error since it's already open.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vyshunavi