[英]Multiprocessing error: AssertionError: cannot start a process twice
假设 function 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__':
保护,以防止无限进程创建
所以,我切换到线程,每次进入 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.