0

대기열의 "생산자"인 상위 프로세스 (예 : 작업 "발송자")와 대기열의 소비자 인 하위 프로세스와 함께 Python 2.7 multiprocessing.Queue을 사용하고 있습니다. (즉, "근로자"프로세스). 부모 프로세스에서 예외가 발생하면 하위 프로세스가 기존 큐 항목을 처리하는 것을 방해하지 않도록 하위 프로세스가 "데몬"이 아니도록해야합니다. 그러나 상위 프로세스에서 예외가 발생하면 상위 프로세스가 하위 프로세스에 참여하려고 시도하기 때문에 교착 상태가 발생하지만 대기열이 비워지면 하위 프로세스는 더 많은 대기열 항목을 영원히 기다립니다. 반면, 나는 자식 프로세스를 "데몬스트릭 (daemonic)"프로세스로 만들면 부모 프로세스가 예외로 종료 될 때 자식 프로세스가 종료되고 자식 프로세스는 기존 큐 항목 처리를 중단합니다.생산자가 예외로 종료 할 때 파이썬 다중 처리 대기열 교착 상태

예 (비 악마의 자식 프로세스를 사용하여) 설명합니다 :

import multiprocessing, time 

def childProcessFunction(sharedQueue): 
    # Consumer loop 
    while True: 
     print "Child process: Consuming item. We will deadlock here if the queue is empty and the parent process has terminated!" 
     consumedItem=sharedQueue.get() 

     print "Child process: Taking some time to process the consumed item..." 
     time.sleep(5) 
     print consumedItem # Do something with (print) the consumed item. 
     time.sleep(5) 
     print "Child process: Done processing the consumed item; re-looping to get another item..." 

def parentProcessMain(): 
    sharedQueue=multiprocessing.Queue() 

    childProcess=multiprocessing.Process(target=childProcessFunction, args=(sharedQueue,)) 

    # Don't want a daemon process because we don't want to interrupt the child's 
    # processing of existing items when the parent process terminates. 
    childProcess.daemon=False 

    print "Parent process: Starting child process..." 
    childProcess.start() 

    failAfter=3 # Simulate a failure after producing 3 items. 

    numDispatched=0 # Number of items put onto the queue. 

    # Producer loop 
    while True: 
     time.sleep(3) 

     if failAfter==0: 
      raise Exception("Parent process encountered a failure of some kind and is exiting uncleanly! Now the parent process will try to join the child process, and the child process will deadlock once the queue is empty!") 

     producedItem="This is item number: %d"%numDispatched 
     print "Parent process: Enqueueing item number %d..."%numDispatched 
     sharedQueue.put(producedItem) 
     numDispatched+=1 
     failAfter-=1 
     time.sleep(3) 

if __name__=="__main__": 
    parentProcessMain() 

내가 자식 프로세스가 기내 무엇이든 큐에 남아있는 어떤 다른 처리를 완료 할 수있는 방법이 있나요 부모 프로세스가 예외 (non-daemonic 동작)를 얻었지만 자식 프로세스가 이후에 영원히 대기열에서 대기하는 것을 방지합니까? 대기열이 생산자가 종료되었고 무한정으로 차단하는 대신 get()에 대한 예외를 throw한다는 사실을 "대기"하는 방법이 있습니까?

부모가 예외를 가져올 때 대기열이 비어 있고 자식 프로세스가 get() 호출에서 대기하는 경우이 경우 예외가 발생한 경우 종료하려면 get()이 필요합니다.

답변

0

하위 프로세스가 완료 될 때까지 예외를 보유 할 수 있습니까? 어쩌면 뮤텍스와 같은 동기화 형식을 통해 가능할 수도 있습니다.