如何可靠地获取twistd衍生过程的pid ?

时间:2023-01-26 16:49:18

I have a Python program that uses psutil to run some various twistd ... commands. twistd spawns and daemonizes a process and writes a foo.pid from which I can read the pid.

我有一个Python程序,它使用psutil运行一些不同的twistd…命令。twistd生成并守护进程并编写foo。pid,我可以从中读取pid。

It also sets up so that this pid-file is cleaned up when the process terminates, which means that it is possible for the spawned process to finish so fast that I cannot read the pid.

它还设置了这个pid文件,以便在进程终止时清理它,这意味着生成的进程有可能很快完成,以至于我无法读取pid。

twistd does return with an error code if it cannot write a pid file, so I can assume that no pid file and no twisted error code means the process was spawned successfully and very quickly terminated, but this whole process seems so flaky. Not to mention having to wait for the pid file to be filled with content while also handling the possibility that it is filled but removed again in one go.

如果twistd不能编写pid文件,那么它会返回一个错误代码,因此我可以假设没有pid文件和twisted error代码意味着进程被成功地并且很快地终止了,但是整个过程看起来是如此的脆弱。更不用说要等待pid文件被内容填充,同时还要处理它被填充但一次又被删除的可能性。

Is there a better way?

有更好的方法吗?

2 个解决方案

#1


2  

There isn't a better way (or if there is, it's very difficult or impossible to know what it is, since you haven't described the general problem you're solving, you've just described a problem you have with a solution you've already selected).

没有更好的方法(或者如果有,很难或者不可能知道它是什么,因为你没有描述你正在解决的一般问题,你只是描述了一个你已经选择的解决方案的问题)。

There's an open feature request for making twistd more amenable to this sort of use, though. If you help resolve it, then you'll be able to do this how you want.

但是,有一个开放的特性要求使twistd更适合这种使用。如果您帮助解决它,那么您将能够按照您希望的方式进行此操作。

#2


0  

I'm not sure what part you're using psutil for, but just calling spawnProcess from Twisted will fire with an IReactorProcess that knows its pid.

我不知道你用psutil做什么,但是仅仅调用Twisted的spawnProcess就会触发一个知道pid的i反应物过程。

If it's twistd you're calling out to, there's definitely a way to do what you'd be doing within the same process too if you'd like.

如果你大声喊出来的是旋转的,那么如果你愿意的话,肯定有办法在同样的过程中做你想做的事情。

#1


2  

There isn't a better way (or if there is, it's very difficult or impossible to know what it is, since you haven't described the general problem you're solving, you've just described a problem you have with a solution you've already selected).

没有更好的方法(或者如果有,很难或者不可能知道它是什么,因为你没有描述你正在解决的一般问题,你只是描述了一个你已经选择的解决方案的问题)。

There's an open feature request for making twistd more amenable to this sort of use, though. If you help resolve it, then you'll be able to do this how you want.

但是,有一个开放的特性要求使twistd更适合这种使用。如果您帮助解决它,那么您将能够按照您希望的方式进行此操作。

#2


0  

I'm not sure what part you're using psutil for, but just calling spawnProcess from Twisted will fire with an IReactorProcess that knows its pid.

我不知道你用psutil做什么,但是仅仅调用Twisted的spawnProcess就会触发一个知道pid的i反应物过程。

If it's twistd you're calling out to, there's definitely a way to do what you'd be doing within the same process too if you'd like.

如果你大声喊出来的是旋转的,那么如果你愿意的话,肯定有办法在同样的过程中做你想做的事情。