How to stop python from propagating signals to subprocesses?

前端 未结 5 1624
情歌与酒
情歌与酒 2020-12-17 08:51

I\'m using python to manage some simulations. I build the parameters and run the program using:

pipe = open(\'/dev/null\', \'w\')
pid = subprocess.Popen(shle         


        
5条回答
  •  无人及你
    2020-12-17 09:50

    I resolved this problem by creating a helper app that I call instead of creating the child directly. This helper changes its parent group and then spawn the real child process.

    import os
    import sys
    
    from time import sleep
    from subprocess import Popen
    
    POLL_INTERVAL=2
    
    # dettach from parent group (no more inherited signals!)
    os.setpgrp()
    
    app = Popen(sys.argv[1:])
    while app.poll() is None:
        sleep(POLL_INTERVAL)
    
    exit(app.returncode)
    

    I call this helper in the parent, passing the real child and its parameters as arguments:

    Popen(["helper", "child", "arg1", ...])
    

    I have to do this because my child app is not under my control, if it were I could have added the setpgrp there and bypassed the helper altogether.

提交回复
热议问题