Daemonising python project that uses Twisted

If we use PEP-3143 and the reference implementation http://pypi.python.org/pypi/python-daemon, then it seems impossible to work with Twisted, because during the demonstration, ALL possible file handlers are explicitly closed, including channels.

When Twisted tries to call os.pipe() and then write to it, it gets the file descriptor destructor.

As I see it, demonization is not suitable for networking with this PEP? And probably the reason why existence twists

Edit:
I should point out that the question is more likely related to "Why does PEP actually make it impossible to create a network application" rather than "How to do it." Twisted breaks these rules to work

+4
source share
4 answers

It does not close all open file descriptors: only those that are not included in the files_preserve attribute. You could probably get this to work by finding out that the PD has become weaker and all open sockets in the reactor, and then passed that to files_preserve ... but why bother? Just use twistd and spin the daemon.

Better yet, use twistd -n and let your process be controlled by some other system tool and not worry about demonization at all.

+2
source

supervisord + upstart

+1
source

Do not use this daemon http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

How to mix it with Twisted see here

http://michael-xiii.blogspot.com/2011/10/twisted.html (warning! Russian text is in front, but Python code is more likely to demonstrate)

+1
source

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.

0
source

All Articles