Multiprocessing error: AssertionError:不能启动一个进程两次 &



假设函数update_values有一些firebase命令,执行大约需要2秒,但我不想停止我的相机并等待。所以我使用多处理,这样相机不会停止,代码仍然更新值,但我面临这个问题。AssertionError: cannot start a process twice我该如何解决这个问题?

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()

哪个操作系统?在Windows上,多进程调用必须由if __name__=='__main__':保护,以防止无限进程创建

Python - Multiprocessing Error '不能启动一个进程两次'

所以,我切换到线程,每次它进入if条件时,我都会创建新的线程…下面是代码…

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()

最新更新