# Opencv multithreading with python

**URL:** <https://forum.opencv.org/t/opencv-multithreading-with-python/4802>\
**Category:** Python\
**Created:** [August 16, 2021, 2:11pm UTC](https://forum.opencv.org/t/opencv-multithreading-with-python/4802 "2021-08-16T14:11:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bogdan\_Khamelyuk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bogdan_khamelyuk/32/2248_2.png) [@Bogdan\_Khamelyuk](https://forum.opencv.org/u/Bogdan_Khamelyuk)\
**Post date:** [August 16, 2021, 2:11pm UTC](https://forum.opencv.org/t/opencv-multithreading-with-python/4802/1 "2021-08-16T14:11:50Z")

</div>

I am planning to take every 5 seconds a frame from the web cam and run all the calculations in separate thread. Unfortunately, after 5 seconds the video transmission stops and nothing happens, although video must be continiued. All I see in the output - is the message “time is now”. But as i tried to debug it, I could never go into the functions in threads.py

So this is my `main.py`:

```auto
from threads import myThread
import cv2 
import time 

vid = cv2.VideoCapture(0)

if __name__ == " __main__": 
    print("starting the measurements")
    threads = {}
    i=0
    
    t_past = t_now = time.time()
    period = 5
    while True:
        _,frame = vid.read()
        t_now = time.time()
        if t_now - t_past >=5 :
            print("time is now")
            t_past=t_now
            threads[i] = myThread(i,frame)
            threads[i].start()
            i+=1
       
        cv2.namedWindow("frame",cv2.WINDOW_NORMAL)
        cv2.imshow("frame",frame)
        
        cv2.waitKey(1)

```

and this is the `threads.py`:

```auto
import threading 
import gauge

class myThread(threading.Thread,gauge.Gauge):

    def __init__ (self,id,frame):
        self.id = id
        threading.Thread. __init__ (self)
        
        self.gg = gauge.Gauge(frame)
        
    def run(self):
        self.gg.makeBlur()
        self.gg.makeGray()
        self.gg.drawCircle()
        self.gg.findAngle()
        self.gg.findPressure()

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 16, 2021, 6:58pm UTC](https://forum.opencv.org/t/opencv-multithreading-with-python/4802/2 "2021-08-16T18:58:23Z")

</div>

crosspost:

> <https://stackoverflow.com/questions/68804095/use-multithreading-for-opencv>
