stderr changes behavior of python's Popen when closing the script

1

I was using this script on python2 to launch an application and then immediately exit the python script without waiting for the child process to end. This was my code:

kwargs = {}
if platform.system() == 'Windows':
    # from msdn [1]
    CREATE_NEW_PROCESS_GROUP = 0x00000200  # note: could get it from subprocess
    DETACHED_PROCESS = 0x00000008          # 0x8 | 0x200 == 0x208
    kwargs.update(creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
    kwargs.update(stderr=subprocess.PIPE)
elif sys.version_info < (3, 2):  # assume posix
    kwargs.update(preexec_fn=os.setsid)
else:  # Python 3.2+ and Unix
    kwargs.update(start_new_session=True)

subprocess.Popen([APP], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,**kwargs)

It works OK on python 2.7, but it doesn't start the expected 'APP' on python 3.7 without changes.

In order to make it work in python3, I found two independent workarounds:

  1. Change stderr to this: stderr=subprocess.DEVNULL

or

  1. Add a time.sleep(0.1) call after the Popen (before closing the script).

I assume this is not actually related to python, but to some event that needs to happen after the process gets opened before the python script can safely exit?

Any hints? I'd really like to know why it happens. Right now I simply added the sleep call.

Thank you

scripting
process
python-3.x
asked on Stack Overflow May 31, 2019 by Emilio M.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0