简体   繁体   English

Python:Popen后退出python.exe吗?

[英]Python: Exiting python.exe after Popen?

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: 我有一个按预期方式运行的python脚本,但当我选择菜单选项以Popen另一个python脚本时除外:

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: 当我运行该代码段(在Windows中)时,子进程将按预期在后台启动,但是当我尝试退出主脚本,但让子进程在后台运行时:

raise SystemExit

...an empty window "c:\\python27\\python.exe" remains. ...一个空窗口“ c:\\ python27 \\ python.exe”仍然存在。 I've tried other EXIT methods with a similar result. 我试过其他EXIT方法,结果相似。 Note: When I exit the primary script without running that snippet, the python window disappears as desired. 注意:当我不运行该代码段而退出主脚本时,python窗口将根据需要消失。

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! 谢谢!

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. 在您长期运行的过程中处理SIGPIPE,请不要死于它。 Live without stdin after the launcher process exits. 启动器进程退出后,无需stdin即可生存。
  • 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. 如果要进行双向通信,请考虑使用命名管道(man mkfifo)或套接字,或编写适当的服务器。
  • 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). 它不会创建“一个完全独立的进程”(python-daemon包会这样做)。 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 在其他情况下,您应该重定向到os.devnull子级的stdin / stdout / stderr,以避免等待终端的输入和/或虚假输出

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM