1

I'm relatively new to python and I've only skimmed the surface of programming so far, which is why I'm confused about daemons. I generally know what they do, but I'm not sure what the best way to implement them is in python. I found this link which demonstrates how to create daemons in python. However, I was wondering if this

#!/usr/bin/env python3.2
import threading

class Update(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        pass        #something you want to run in the background

continous = Update
continous.daemon = True
continous.start()

would be just as effective?

Lobabob
  • 132
  • 8

1 Answers1

0

From threading documentation: "The entire Python program exits when no alive non-daemon threads are left". Daemon thread will just be terminated upon application finish.

In order to implement a system daemon in python you should use os.fork. Take a look at the example of simple daemon.

Vladimir
  • 9,913
  • 4
  • 26
  • 37
  • What you quoted is something I already had in mind when I posted my question. The code sample I provided creates a daemon thread that will run in the background after the main thread exits. – Lobabob Aug 14 '12 at 14:11
  • You're a bit wrong about thread will run after main thread exits since the whole main process is terminated and no new (daemon) process were created. If it were so then the code ( http://pastebin.com/646t71Ba ) would print message to /tmp/log.txt each second indefinitely while the log file remains (almost) empty. – Vladimir Aug 14 '12 at 15:06