简体   繁体   中英

Multiprocessing error: AssertionError: cannot start a process twice

Let's say that the function update_values have some firebase commands which take about 2 secs to execute but I don't want to stop my camera and wait for that. So I used multiprocessing, so that the camera doesn't stop and the code still updates the value, but I'm facing this issue. AssertionError: cannot start a process twice How should I resolve this?

import cv2
import numpy as np
from firebase import firebase
from firebase.firebase import FirebaseApplication
import multiprocessing

def update_values():
    x=np.random.randint(100)
    print("Updating Values") 
    print("done")

cap=cv2.VideoCapture(0)


#for improving fps
flag=0
#establishing connnection with the database for the first time
firebase_message = firebase.FirebaseApplication("<link to database>", None)    #reconnecting to the existing table
print("Connectionion Established")

t1 = multiprocessing.Process(target=update_values)

count_down=0
count_up=0
while(cap.isOpened()):
    _,frame=cap.read()
    x=np.random.randint(100)
    if(flag%100==0):
            t1.start()
    flag+=1
    cv2.imshow('frame',result)
    k = cv2.waitKey(1) & 0xff
    if k == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

which operating system? On Windows, multiprocessing calls must be protected by if __name__=='__main__': to prevent infinite process creation

Python - Multiprocessing Error 'cannot start a process twice'

So, I switched to threading and I'm making new threads every time it enters the if condition...here is the code...

import cv2
import numpy as np
from firebase import firebase
from firebase.firebase import FirebaseApplication
import threading

cap=cv2.VideoCapture(0)

#for improving fps
flag=0
#establishing connnection with the database for the first time
firebase_message = firebase.FirebaseApplication("<link to database>", None)    #reconnecting to the existing table
print("Connectionion Established")

def update_values():
    x=np.random.randint(100)
    print("Updating Values")
    print("done")



count_down=0
count_up=0
while(cap.isOpened()):
    _,frame=cap.read()
    
    x=np.random.randint(100)
    
    if(flag%100==0):
            t1 = threading.Thread(target=update_values)    
            t1.start()
            print("Done")
    flag+=1
    cv2.imshow('frame',result)
    k = cv2.waitKey(1) & 0xff
    if k == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM