1

Small nagging issue:

I have a python script that is working as expected, except when I select a menu option to Popen another python script:

myPath = r"c:\Python27\myScript.py"
cmd = r"c:\Python27\python.exe '{}'".format(myPath)
py_process = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=STDOUT)

When I run that snippet (in windows), the child process is kicked-off in the background as expected, but when I attempt to exit the primary script, but leave the child process running in the background:

raise SystemExit

...an empty window "c:\python27\python.exe" remains. I've tried other EXIT methods with a similar result. Note: When I exit the primary script without running that snippet, the python window disappears as desired.

My goal is to leave no trace/window once the primary script is exited in all cases, but child process should remain running in background.

Any suggestions to accomplish this goal?

Thanks!

Mark Pelletier
  • 1,329
  • 2
  • 24
  • 42

1 Answers1

0

If you want to first communicate to the started process and then leave it alone to run further, you have a few options:

  • Handle SIGPIPE in your long-running process, do not die on it. Live without stdin after the launcher process exits.
  • Pass whatever you wanted using arguments, environment, or a temporary file.
  • If you want bidirectional communication, consider using a named pipe (man mkfifo) or a socket, or writing a proper server.
  • Make the long-running process fork after the initial bi-direcional communication phase is done.

It does not create "a completely independent process" (that what python-daemon package does). In other cases you should redirect to os.devnull child's stdin/stdout/stderr to avoid waiting for input and/or a spurious output to the terminal

Source

Community
  • 1
  • 1
umbreon222
  • 247
  • 2
  • 8
  • Thanks, umbreon222. There is no need for the parent to communicate with the child process whatsoever. I do notice manually killing the "conhost.exe" process in task manager does remove that empty window, and leaves the backgroung running. – Mark Pelletier Jun 18 '15 at 13:32
  • Adding:pykill=subprocess.Popen("TASKKILL /IM "+ " conhost.exe" + " /F") just before "raise SystemExit" kill the empty window, but feels risky to me as it kill ALL conhost processes There must be a better way. – Mark Pelletier Jun 18 '15 at 13:54
  • 2
    Instead of calling python.exe as your interpreter try pythonw.exe. It's the windowless python interpreter. (Also if you aren't going to communicate with the child then don't hook it with pipes) – umbreon222 Jun 18 '15 at 14:27