thesharp/daemonize — pull request #5 by adamdonahue (2013-07-23 17:38:03+00:00)
Title: Added a new Daemonizer class

Added a Daemonizer class, which fixes some bugs in Daemonize and does away with the keep_fds and specific file descriptor handling. (The caller should just do this, rather than telling the daemonizer which ones to skip, which seems like the wrong model.)

It also addresses a few bugs:
- The existing lockFile code wipes out the PID before checking whether the file is locked; changed to append, not write.
- The file descriptor stuff introduces some funky bugs, and doesn't handle the case where a user actually passes in sys.stdin, sys.stdout, and/or sys.stderr. keep_fds should either require all descriptors (not handle those three specially). always do those three and not process them, or -- best -- just follow the normal daemonizing convention, detaching 0, 1, and 2, and not touching anything else. In order words, keep_fds is asking for trouble, requires more work from the user, and should be axed.

Also adds reasonable defaults for app (now called 'name') and pid (now called lockFile), and makes func the first parameter, so a user can just have something like:

def httpd(self):
...

def run(self):
# Open log files, etc.
daemon = Daemonizer(self.httpd)
daemon.start()
# Perhaps do other stuff.

I didn't integrate this is Daemonize mainly because of the file descriptor handling piece. It started to make the code a bit convoluted. If you wanted to integrate I'd suggest extracting that piece to its own method, modifying Daemonize to inherit from Daemonizer, adding __init__ to match current signature, and then override the file descriptor handler to use the original logic. Something like that.

Feel free to just pick and choose among the changes, but I made them for a project and figured I'd contribute them back.

Thanks.

Adam