The practice of closing all open file descriptors is a consequence of the fact that the deamonization process inherits some open files from the parent process. For example, you can open dozens of files in one process (with, say, os.open()
) or then call a subprocess that inherits them. You probably don't have an easy way, as a subprocess, to find out which filedescriptors are useful from the parent process (unless you pass this along with the command line arguments), and you certainly don't want stdin, stdout or stderr, so its completely itβs reasonable to close all open files before doing anything.
The deamonization process will then take some additional steps to become a deamon (as indicated in the PEP).
Once a process is completely separate from any type of terminal, it can start opening files and connections as needed. It will open its log files, configuration files, and network connections.
Others mentioned that twisted using the twistd
tool already does a pretty good job of all this, and you don't need to use an add-on module. If you do not want to use twistd
(for some reason), but you want to use twisted, you can use something external, but first you need to demonize and then import the twisted and the rest of the application code and open the network connections last.
source share