ZooKeeper
ZooKeeper

Reputation: 715

Daemonising python project, that uses Twisted

If we use PEP-3143 and it's reference implementation http://pypi.python.org/pypi/python-daemon then it looks like impossible to have Twisted working, since during daemonising ALL possible file handlers are explicitly closed, which includes pipes.

When Twisted tries to call os.pipe() and then write to it - gets bad file descriptor.

As I see it, daemonising is not suited for networking by this PEP? And probably that's the reason why twisted exist

Edit:
I'll have to point out that the question is more of the "Why PEP effectively makes it impossible to create a network application" rather then "How to do it". Twisted breaks this rules in order to work

Upvotes: 4

Views: 1042

Answers (4)

user221014
user221014

Reputation:

supervisord + upstart

Upvotes: 1

SingleNegationElimination
SingleNegationElimination

Reputation: 156138

The practice of closing all open filedescriptors is an effect of the possibility that the deamonizing 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 and then invoke a sub-process that inherits them. You probably don't have an easy way, as a subprocess, to know what filedescriptors are useful from the parent process (unless you pass that along with command line arguments), and you certainly don't want stdin, stdout or stderr, so its perfectly reasonable to, before doing anything else, close all open files.

Then a deamonizing process will take some additional steps to become a deamon (as laid out in the PEP).

Once the process is fully detached from any kind of terminal, it can start opening files and connections as it needs. It'll open its log files, its configuration files, and its network connections.

Others have mentioned that twisted, via the twistd tool already does a pretty good job of all of this, and you don't need to use an extra module. If you don't want to use twistd (for some reason) but you do want to use twisted, you could use something external, but you should deamonize first and then import twisted and the rest of your application code and open network connections last.

Upvotes: 0

Michael_XIII
Michael_XIII

Reputation: 175

Feel free to 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 ahead, but Python code is rather demonstrating)

Upvotes: 1

Glyph
Glyph

Reputation: 31860

It doesn't close all the open file descriptors: just the ones not in the files_preserve attribute. You could probably coerce this to work by figuring out the FD of the waker and all open sockets in the reactor and then passing that to files_preserve... but why bother? Just use twistd and have twisted daemonize itself.

Better yet, use twistd -n and let your process get monitored by some other system tool, and don't bother with daemonization at all.

Upvotes: 2

Related Questions