2

나는 A와 B라는 두 가지 기능을 가진 Python 스크립트 (RHEL을 기반으로하는 유닉스 계열)를 가지고 있는데,이 스크립트는 A와 B라는 두 가지 기능을 가지고 있습니다. 서로 다른 독립적 인 프로세스 기능를 계속 기능 A에서 데이터를 전달는 의도적으로 파이썬에서 고아 프로세스 만들기

  • 동안 함수 B의 실행을 B로,

    • 시작 스크립트로 myscripf
    • 스폰에게 기능에게 새로운 프로세스를 실행합니다 (B를 분리 및 A)
    • 함수 A가 완료되면 B가 여전히 실행 중일지라도 MyScript를 종료하십시오.

    멀티 프로세스를 사용하여 데몬 프로세스를 작성해야한다고 생각했지만 documentation은 올바른 용도가 아니라고 제안합니다. 그래서, 나는 자식 프로세스와 자식^2 프로세스 (자식의 자식)를 생성하고 자식을 강제 종료하도록 결정했습니다. 이 해결 방법이 작동하는 것처럼 보이지만 실제로는보기 흉한 것처럼 보입니다.

    더 파이썬으로 만들 수 있습니까? 서브 프로세스 모듈은 함수에서 작동 할 메소드를 가지고 있습니까? 아래 샘플 코드. 여기

    import multiprocessing 
    import time 
    import sys 
    import os 
    
    def parent_child(): 
        p = multiprocessing.current_process() 
        print 'Starting parent child:', p.name, p.pid 
        sys.stdout.flush() 
        cc = multiprocessing.Process(name='childchild', target=child_child) 
        cc.daemon = False 
        cc.start() 
        print 'Exiting parent child:', p.name, p.pid 
        sys.stdout.flush() 
    
    def child_child(): 
        p = multiprocessing.current_process() 
        print 'Starting child child:', p.name, p.pid 
        sys.stdout.flush() 
        time.sleep(30) 
        print 'Exiting child child:', p.name, p.pid 
        sys.stdout.flush() 
    
    def main(): 
        print 'starting main', os.getpid() 
        d = multiprocessing.Process(name='parentchild', target=parent_child) 
        d.daemon = False 
        d.start() 
        time.sleep(5) 
        d.terminate() 
        print 'exiting main', os.getpid() 
    
    main() 
    
  • +0

    사용중인 OS는 무엇입니까? – Keith

    +0

    @Keith, 방금 질문을 업데이트했습니다. 유닉스처럼. – Peter

    답변

    2

    은 단일 통화 spawn_detached(callable)로 기능을 이동 원래 코드의 임의의 버전입니다. 프로그램이 종료 된 후에도 분리 된 프로세스가 계속 실행됩니다.

    import time 
    import os 
    from multiprocessing import Process, current_process 
    
    def spawn_detached(callable): 
        p = _spawn_detached(0, callable) 
        # give the process a moment to set up 
        # and then kill the first child to detach 
        # the second. 
        time.sleep(.001) 
        p.terminate() 
    
    def _spawn_detached(count, callable): 
        count += 1 
        p = current_process() 
        print 'Process #%d: %s (%d)' % (count, p.name, p.pid) 
    
        if count < 2: 
         name = 'child' 
        elif count == 2: 
         name = callable.func_name 
        else: 
         # we should now be inside of our detached process 
         # so just call the function 
         return callable() 
    
        # otherwise, spawn another process, passing the counter as well 
        p = Process(name=name, target=_spawn_detached, args=(count, callable)) 
        p.daemon = False 
        p.start() 
        return p 
    
    def operation(): 
        """ Just some arbitrary function """ 
        print "Entered detached process" 
        time.sleep(15) 
        print "Exiting detached process" 
    
    
    if __name__ == "__main__": 
        print 'starting main', os.getpid() 
        p = spawn_detached(operation) 
        print 'exiting main', os.getpid() 
    
    +0

    프로그램이 종료되지 않습니다. [내 대답의 코드] (http://stackoverflow.com/a/13096616/4279)를 실행하십시오. 그것은 (.daemon = False를 가진) 자식 프로세스가 메인 스레드에서 빠져 나가는 것을 보여줍니다. 아니면 (최상위 코드에서) pid를 찾을 수 있습니다. – jfs

    +0

    @ J.F.Sebastian : 당신이 무슨 말을하고 있는지 잘 모르겠습니다. 프로그램은 쉘로 즉시 빠져 나오고 원래 PID는 더 이상 존재하지 않습니다. 단지'operation' PID 만 존재합니다. – jdi

    +0

    네 말이 맞아. 그돈은 거기 있지 않아. – jfs