简体   繁体   English

多处理错误:AssertionError:无法启动一个进程两次

[英]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.假设 function update_values 有一些 firebase 命令需要大约 2 秒才能执行,但我不想停止相机并等待。 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? 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()

which operating system?哪个操作系统? On Windows, multiprocessing calls must be protected by if __name__=='__main__': to prevent infinite process creation在 Windows 上,多处理调用必须受if __name__=='__main__':保护,以防止无限进程创建

Python - Multiprocessing Error 'cannot start a process twice' Python - 多处理错误“无法启动一个进程两次”

So, I switched to threading and I'm making new threads every time it enters the if condition...here is the code...所以,我切换到线程,每次进入 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.

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