簡體   English   中英

多處理錯誤:AssertionError:無法啟動一個進程兩次

[英]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__':保護,以防止無限進程創建

Python - 多處理錯誤“無法啟動一個進程兩次”

所以,我切換到線程,每次進入 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