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

Python中的多处理和Join()问题

  •  0
  • batuman  · 技术社区  · 6 年前

    我有三个过程。

    主进程已启动批处理流处理。

    当主进程用Ctrl+C键中断时,批处理流进程和CameraProcess进程中的while循环将停止。他们应该一个接一个地回来。现在不是了。

    有什么问题吗?

    我的测试代码是

    import multiprocessing as MultiProcess
    
    
    def CameraProcess(stopbit):
        while (not stopbit.is_set()):
            test1=1
    
    
    
    def BatchStreaming(stopbit):
        camProcess = MultiProcess.Process(target=CameraProcess, args=(stopbit,))
        camProcess.start()
        while (not stopbit.is_set()):
            test1=1
        camProcess.join() 
        print('cam process join')
    
    class ProcessPipeline:
        def __init__(self):
            #Current Cam
            self.batchStreaming = None
            self.stopbit = MultiProcess.Event() 
        def BatchProcessing(self):
            self.batchStreaming = MultiProcess.Process(target=BatchStreaming, args=(self.stopbit,))
            self.batchStreaming.start()
            try:
                while(self.stopbit is not None):
                    test=1
    
            except KeyboardInterrupt:
    
                print('Caught Keyboard interrupt')
    
            except:
                e = sys.exc_info()
                print('Caught Main Exception')
                print(e)
    
    
            self.StopStreaming()
    
    
        def StopStreaming(self):
            print('in stopCamStream')
            self.stopbit.set()
            if not self.batchStreaming.is_alive():
                self.batchStreaming.join() 
                print("batchProcess.join()")
            print('All terminated')
    
    if __name__ == "__main__":
        mc = ProcessPipeline()
        mc.BatchProcessing()
    
    0 回复  |  直到 6 年前