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

2024-09-28 01:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个过程

主进程启动了批处理流处理

BatchStreaming进程已启动CameraProcess进程

当主进程用Ctrl+C键中断时,BatchStreaming进程和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()

Tags: self进程isdefnotprintjoinset

热门问题