How not to kill a subprocess opened by another python file?

Asked

Viewed 40 times

0

I would like from a script in python to run a program I developed in python as well. However I would like to terminate my script but not the program I opened by it.

I’m opening my program for a subprocess like this:

self.davros_p = subprocess.Popen(
        [python_path, "-m", "meu_programa", "start"],
        stdout=open(os.devnull, 'w'),
        stderr=open(os.devnull, 'w')
    )

Both run perfectly, however much I kill my script, I end up killing my program along. I would have some way of making sure that my program wasn’t finalized together?

1 answer

0


Apparently this is a problem in Windows. When a Signal is given to kill the script, that Signal is passed to the subprocess, which is the meu_program. One way to solve this is to include "creationflags=subprocess.CREATE_NEW_PROCESS_GROUP" the list of Popen parameters.

self.proc = subprocess.Popen(
    [python_path, "-m", "meu_programa", "start"],
    stdout=open(os.devnull, 'w'),
    stderr=open(os.devnull, 'w'),
    creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,

)

Windows passes Signal to all processes of the same group, so including creationflags you create a new group for the subprocess of the meu_program and it will not be killed when you want to kill only the script.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.