Python产生一个子子进程,分离并退出 [英] Python spawn off a child subprocess, detach, and exit

查看:514
本文介绍了Python产生一个子子进程,分离并退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否是执行系统进程并与父进程分离的正确方法,尽管允许父进程退出而不创建僵尸和/或杀死子进程.我目前正在使用子流程模块并执行此操作...

I'm wondering if this is the correct way to execute a system process and detach from parent, though allowing the parent to exit without creating a zombie and/or killing the child process. I'm currently using the subprocess module and doing this...

os.setsid() 
os.umask(0) 
p = subprocess.Popen(['nc', '-l', '8888'],
                     cwd=self.home,
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.STDOUT)

os.setsid()更改了进程组,我相信这是使进程在其父级退出时继续运行的原因,因为它不再属于同一进程组.

os.setsid() changes the process group, which I believe is what lets the process continue running when it's parent exits, as it no longer belongs to the same process group.

这是正确的吗,这也是执行此操作的可靠方法吗?

Is this correct and also is this a reliable way of performing this?

基本上,我有一个远程控制实用程序,该实用程序通过套接字进行通信并允许远程启动进程,但是我必须确保如果远程控制死了,则它启动的进程将继续运行而不会受到影响.

Basically, I have a remote control utility that communicate through sockets and allows to start processes remotely, but I have to ensure that if the remote control dies, the processes it started continue running unaffected.

我正在阅读有关双叉的信息,不确定是否有必要和/或子流程.POpenclose_fds以某种方式解决了这个问题,而所需要做的只是更改流程组?

I was reading about double-forks and not sure if this is necessary and/or subprocess.POpen close_fds somehow takes care of that and all that's needed is to change the process group?

谢谢.

Ilya

推荐答案

popen在Unix上是

popen on Unix is done using fork. That means you'll be safe with:

  1. 您在父进程中运行Popen
  2. 立即退出父进程

退出父进程时,子进程由init进程(在OSX上为launchd)继​​承,并且仍将在后台运行.

When the parent process exits, the child process is inherited by the init process (launchd on OSX) and will still run in the background.

不需要python程序的前两行,这非常有用:

The first two lines of your python program are not needed, this perfectly works:

import subprocess
p = subprocess.Popen(['nc', '-l', '8888'],
                     cwd="/",
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)

我正在阅读有关双叉的信息,不确定是否有必要

I was reading about double-forks and not sure if this is necessary

如果您的父进程继续运行,并且您需要保护自己的孩子免于与父死亡,这将是必需的. 这个答案显示了如何做到这一点.

This would be needed if your parent process keeps running and you need to protect your children from dying with the parent. This answer shows how this can be done.

双叉的工作原理:

  1. 通过os.fork()
  2. 创建一个孩子
  3. 在此子调用Popen()中,它将启动长时间运行的进程
  4. 退出子进程:Popen进程由init继承并在后台运行
  1. create a child via os.fork()
  2. in this child call Popen() which launches the long running process
  3. exit child: Popen process is inherited by init and runs in the background

为什么父母必须立即退出?如果不立即退出会怎样?

Why the parent has to immediately exit? What happens if it doesn't exit immediately?

如果让父级运行,而用户停止该进程,例如通过ctrl-C(SIGINT)或ctrl-\(SIGQUIT),它将杀死父进程和Popen进程.

If you leave the parent running and the user stops the process e.g. via ctrl-C (SIGINT) or ctrl-\ (SIGQUIT) then it would kill both the parent process and the Popen process.

如果分叉后一秒钟退出,该怎么办?

What if it exits one second after forking?

然后,在这1秒钟内,您的Popen进程容易受到ctrl-c等的攻击.如果您需要100%确定,请使用双叉.

Then, during this 1s period your Popen process is vulnerable to ctrl-c etc. If you need to be 100% sure then use the double forking.

这篇关于Python产生一个子子进程,分离并退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆