I need to run a daemon process from a python django module which will be running an xmlrpc server. The main process will host an xmlrpc client. I am a bit confused regarding creating, starting, stopping and terminating daemons in python. I have seen two libraries, the standard python multiprocessing, and another python-daemon (https://pypi.python.org/pypi/python-daemon/1.6), but not quite understanding which would be effective in my case. Also when and how do I need to handle SIGTERM for my daemons? Can anybody help me to understand these please?
1 Answers
The multiprocessing
module is designed as a drop-in replacement for the threading module. It's designed to be used for the same kind of tasks you'd normally use threads for; speeding up execution by running against multiple cores, background polling, and any other task that you want running concurrently with some other task. It's not designed to launch standalone daemon processes, so I don't think it's appropriate for your use-case.
The python-daemon
library is designed to "daemonize" the currently running Python process. I think what you want is to use the subprocess
library from your main process (the xmlrpc client) to launch your daemon process (the xmlrpc server), using subprocess.Popen
. Then, inside the daemon process, you can use the python-daemon
library to become a daemon.
So in the main process, something like this:
subprocess.Popen([my_daemon.py, "-o", "some_option"])
And in my_daemon.py
:
import daemon
...
def main():
# Do normal startup stuff
if __name__ == "__main__":
with daemon.DaemonContext(): # This makes the process a daemon
main()

- 91,354
- 19
- 222
- 219
-
Thank you, but I tried differently. I forked a child from main program and created a xmlrpc server in the child and xmlrpcclient in the main process as shown here `https://docs.python.org/2/library/xmlrpclib.html` in section 20.23.2 But the remote function that the server exposes is written in a different file which I import. As a standalone program this setup works fine, when I am doing the same thing in django, it gives Broken PIPE (error 32). Can you tell me what is the problem with django? – Sayantan Jul 09 '14 at 13:00