叉并忽略子出口状态 [英] Fork and ignore child exit status

查看:75
本文介绍了叉并忽略子出口状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在编写一个守护程序,该守护程序会自动更新多个长期运行的程序.我不在乎程序的退出状态,因为孩子退出的唯一方法是在升级时强制退出他们.不幸的是,结果是我创建了大量的僵尸进程.

While currently writing a daemon which automatically updates multiple long running programs. I don't care about the program's exit status as the only way the child will exit is when force quitting them on upgrade. Unfortunately I'm creating a large number of zombie processes as a result.

我知道我可以使用waitpid(0, WNOHANG)来侦听任何子级的更改(这几乎正是我想要的),但是我还使用python multiprocessing模块在其中运行多个单独的升级过程因此,可以运行waitpid的长期运行的进程实际上是终止进程的祖父母,因此它永远不会收到任何信号.

I'm aware that I can use waitpid(0, WNOHANG) to listen for changes from any of the children (which is almost exactly what I want), however I'm also using the python multiprocessing module to run multiple separate upgrade processes in parallel, so the long running process which I could run waitpid from is actually the grandparent of the terminated process and therefore it never receives any signals.

那么,有没有办法派生一个子进程,这样我就不需要检查退出状态,还是只需要处理充满僵尸的进程表?

So, is there any way to fork a child process so that I never need to check the exit status, or do I just have to deal with my process table being filled with zombies?

推荐答案

您应该可以通过SIGCHLD的信号处理程序的设置来控制它.

You should be able to control this with the setting of the signal handler for SIGCHLD.

在C语言中,您将使用:

In C, you would use:

signal(SIGCHLD, SIG_IGN);

您需要使它适应Python界面.

You'll need to adapt that to the Python interface.

您可以使用Python 2 信号或Python 3 信号模块;他们似乎对手头的问题是相同的:

You can use the Python 2 signal or Python 3 signal module; they seem to be identical for the problem on hand:

import signal

signal.signal(signal.SIGCHLD, signal.SIG_IGN)

有关底层系统行为的信息,请参见:POSIX 信号概念 (尤其是该部分下的信号操作"和SIG_IGN).

See: POSIX Signal Concepts for the underlying system behaviour (in particular, 'Signal Actions' and SIG_IGN under that section).

警告:未经正式测试的Python代码!

这篇关于叉并忽略子出口状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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