簡體   English   中英

python 多處理在不終止整個程序的情況下第二次運行速度較慢

[英]python multiprocessing runs slower at second time without terminating whole program

我有一個帶有opencv gui的 python 程序,它在 class 實例中的線程中使用多處理。

運行以下腳本后,當我鍵入q時,它將退出子循環並終止或加入模塊中的所有現有線程、進程和關閉隊列。

然后如果我輸入r然后它會再次啟動子循環,但這一次,多處理中的工作人員運行速度慢了兩倍。 我不知道為什么會這樣。

代碼如下。

    module_started = False
    height = 480
    width = 800
    img_base = np.zeros((height,width,3), np.uint8)
    cv2.namedWindow("VIN", cv2.WINDOW_NORMAL)
    
    while True:
        
        if not module_started:
            cam1 = PersonDetector(0)
            cam1.start()
            outputs = []
            module_started = True
    
        while True:
     
                if cam1.new_detection:
                    outputs = cam1.get_output()
                
                if outputs:
                    
                    for xys in outputs:
                        (startX, startY, endX, endY) = xys
                        cv2.rectangle(cam1.frame, (startX, startY), (endX, endY), (255,5,150),2)
                
    
                # show the output image
                cv2.imshow("VIN", cam1.frame)
                key = cv2.waitKey(40) & 0xFF
                # if the `q` key was pressed, break from the loop
                if key == ord("q"):
                    break
                
            cam1.stop()
            del cam1
        
        # show the output frame
        
        cv2.imshow("VIN", img_base)
        key = cv2.waitKey(40) & 0xFF
      
        if key == ord("e"):          
            break
        if key == ord("r"):             # go back to start sub loop
            module_started = False
    
    cv2.destroyAllWindows()

它在樹莓派 4 上。它有 cpu 4 核心。 第一次,htop 顯示 CPU% 超過 300%,但第二次,htop 顯示 CPU% 僅為 99 或 100%...

為什么會發生?

我自己測試了一整天。 這不是因為多處理也不是線程。

這是因為 cv2.imshow(),我刪除了 imshow 然后一切都保持相同的速度但是當像上面的代碼一樣重復 imshow 時,它顯示程序關閉,我仍然不知道為什么......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM