pythonlinuxtwistedpidtwistd

How to reliably get pid of twistd spawned process?


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.

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.

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.

Is there a better way?


Solution

  • 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.