代码之家  ›  专栏  ›  技术社区  ›  Amrmsmb

.start()不会启动进程并导致程序崩溃

  •  0
  • Amrmsmb  · 技术社区  · 4 年前

    在下面发布的代码中,我正在对Process进行子类化。正如一些教程中所述,必须调用.start才能启动工作流程。 我面临的问题是,我无法调用.start()方法,因为它会导致应用程序崩溃并生成下面发布的错误消息。相反,我调用.run()方法。 请告诉我为什么我不能使用.start(),它是如何生成错误消息的,以及如何修复它,这样我就可以使用/start()

    笔记 :下面发布的代码位于Web服务中

    错误消息 :

    File "C:\Python310\lib\multiprocessing\process.py", line 100, in _check_closed
        if self._closed:
    AttributeError: 'KeyGridCellsProcessing' object has no attribute '_closed'
    

    密码 :

    class KeyGridCellsProcessing(Process):
    
        @staticmethod
        def newInstance(pNameSuffix,row,col,_pixelsValuesSatisfyThresholdInWindowedSegment,pixelsValuesSatisfyThresholdInTIFFImageDatasetCnt):
            keyGridCellsProcessing = KeyGridCellsProcessing(pNameSuffix,row,col,_pixelsValuesSatisfyThresholdInWindowedSegment,pixelsValuesSatisfyThresholdInTIFFImageDatasetCnt)
            KeyGridCellsProcessing.processesCollector.append(KeyGridCellsProcessing)
            return keyGridCellsProcessing
        
        @staticmethod
        def setDatasetElevationsTIFFInEPSG25832(datasetElevationsTIFFInEPSG25832):
            KeyGridCellsProcessing.datasetElevationsTIFFInEPSG25832 = datasetElevationsTIFFInEPSG25832
        
        @staticmethod
        def setNDVIsTIFFWindowedSegmentContentsInEPSG25832(NDVIsTIFFWindowedSegmentContentsInEPSG25832):
            KeyGridCellsProcessing.NDVIsTIFFWindowedSegmentContentsInEPSG25832 = NDVIsTIFFWindowedSegmentContentsInEPSG25832
            
        @staticmethod
        def setMainTIFFImageDatasetContents(mainTIFFImageDatasetContents):
            KeyGridCellsProcessing.mainTIFFImageDatasetContents = mainTIFFImageDatasetContents
        
        @staticmethod
        def waitForProcessToFinish():
            for p in KeyGridCellsProcessing.processesCollector:
                p.join()  
                # logger.debug(f"process:{p}")
            # exit()
                
        def __init__(self,pNameSuffix,row,col,_pixelsValuesSatisfyThresholdInWindowedSegment,pixelsValuesSatisfyThresholdInTIFFImageDatasetCnt):
            # self.queue = queue
            self.pNameSuffix = pNameSuffix
            self.row = row
            self.col = col
            self._pixelsValuesSatisfyThresholdInWindowedSegment = _pixelsValuesSatisfyThresholdInWindowedSegment
            self.pixelsValuesSatisfyThresholdInTIFFImageDatasetCnt = pixelsValuesSatisfyThresholdInTIFFImageDatasetCnt
            self.fourCornersOfKeyWindowInEPSG4326 = []
            
        def run(self):
            runStartTime = time.time()
            #logic
            ...
            ...
            runEndTime = time.time() - runStartTime
            KeyGridCellsProcessing.runMethodForKeyGridCellsExecutionTimeAccumulator+=runEndTime
        
        @staticmethod
        def enqueue():
            KeyGridCellsProcessing.keyGridCellsQueue.put([KeyGridCellsProcessing.NDVIsPer10mX10mForKeyWindow,
                                    ......,
                                    ......,
                                    ......,
                                    ......,
                                    ......,
                                    ], block=True, timeout=None)
        @staticmethod
        def dequeue():
            deqeueStartTime = time.time()
            item = KeyGridCellsProcessing.keyGridCellsQueue.get(block=True, timeout=None)
            KeyGridCellsProcessing.executionTimeOfDequeueProcess = time.time() - deqeueStartTime
            return item
        
        
        @staticmethod
        def shutdownQueue():
            KeyGridCellsProcessing.keyGridCellsQueue.close()
            KeyGridCellsProcessing.keyGridCellsQueue.join_thread()
    
        def startProcessing(self):
            #self.start()#<=========== causes the app to crash
            self.run()  #works
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Guy    4 年前

    start() 第一个动作是打电话 self._check_closed() 在里面 BaseProcess (的基类 Process )

    def _check_closed(self):
        if self._closed:
            raise ValueError("process object is closed")
    

    self._closed 在中初始化 基本流程 __init__ ,但既然你没有 super().__init__() 在您的类中,它从未初始化过。添加它应该可以解决问题

    class KeyGridCellsProcessing(Process):
        def __init__(self, ...):
            super().__init__()
            ...