process_helper.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. # A program which exits after starting a child which inherits its
  2. # stdin/stdout/stderr and keeps them open until stdin is closed.
  3. import sys, os
  4. def grandchild():
  5. sys.stdout.write('grandchild started')
  6. sys.stdout.flush()
  7. sys.stdin.read()
  8. def main():
  9. if sys.argv[1] == 'child':
  10. if sys.argv[2] == 'windows':
  11. import win32api as api, win32process as proc
  12. info = proc.STARTUPINFO()
  13. info.hStdInput = api.GetStdHandle(api.STD_INPUT_HANDLE)
  14. info.hStdOutput = api.GetStdHandle(api.STD_OUTPUT_HANDLE)
  15. info.hStdError = api.GetStdHandle(api.STD_ERROR_HANDLE)
  16. python = sys.executable
  17. scriptDir = os.path.dirname(__file__)
  18. scriptName = os.path.basename(__file__)
  19. proc.CreateProcess(
  20. None, " ".join((python, scriptName, "grandchild")), None,
  21. None, 1, 0, os.environ, scriptDir, info)
  22. else:
  23. if os.fork() == 0:
  24. grandchild()
  25. else:
  26. grandchild()
  27. if __name__ == '__main__':
  28. main()