New version: 2.1.1.

- Fix tests.
- Fix empty self.keep_fds behaviour.
- Add tests for self.keep_fds.
- Update README.md.
This commit is contained in:
Ilya A. Otyutskiy 2012-09-24 19:26:51 +04:00
parent 0f2f842a81
commit 10bfa1c243
5 changed files with 64 additions and 5 deletions

View File

@ -25,4 +25,23 @@ You can install it from Python Package Index (PyPI):
daemon.start()
## File descriptors
Daemonize object's constructor understands the optional argument **keep_fds** which can contain a list of fds which should remain open. TODO: explain it more.
Daemonize object's constructor understands the optional argument **keep_fds** which contains a list of FDs which should not be closed. For example:
import logging
from daemonize import Daemonize
pid = "/tmp/test.pid"
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False
fh = logging.FileHandler("/tmp/test.log", "w")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
keep_fds = [fh.stream.fileno()]
def main():
logger.debug("Test")
daemon = Daemonize(app="test_app", pid=pid, action=main, keep_fds=keep_fds)
daemon.start()

View File

@ -13,10 +13,10 @@ from logging import handlers
class Daemonize(object):
""" Daemonize object
Object constructor expects three arguments:
- app: contains the application name which will be sent to syslog
- pid: path to the pidfile
- app: contains the application name which will be sent to syslog.
- pid: path to the pidfile.
- action: your custom function which will be executed after daemonization.
- keep_fds: optional list of fds which should remain open
- keep_fds: optional list of fds which should not be closed.
"""
def __init__(self, app, pid, action, keep_fds=None):
self.app = app
@ -24,6 +24,8 @@ class Daemonize(object):
self.action = action
if keep_fds:
self.keep_fds = keep_fds
else:
self.keep_fds = []
# Initialize logging.
self.logger = logging.getLogger(self.app)
self.logger.setLevel(logging.DEBUG)

View File

@ -4,7 +4,7 @@ from setuptools import setup, find_packages
setup(
name="daemonize",
version="2.1",
version="2.1.1",
py_modules=["daemonize"],
author="Ilya A. Otyutskiy",
author_email="sharp@thesharp.ru",

20
tests/daemon_keep_fds.py Normal file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python
import logging
from daemonize import Daemonize
pid = "/tmp/test.pid"
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False
fh = logging.FileHandler("/tmp/test.log", "w")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
keep_fds = [fh.stream.fileno()]
def main():
logger.debug("Test")
daemon = Daemonize(app="test_app", pid=pid, action=main, keep_fds=keep_fds)
daemon.start()

View File

@ -29,5 +29,23 @@ class DaemonizeTest(unittest.TestCase):
sleep(10)
self.assertTrue(os.path.isfile(self.pidfile))
class KeepFDsTest(unittest.TestCase):
def setUp(self):
self.pidfile = "/tmp/test.pid"
self.logfile = "/tmp/test.log"
os.system("python tests/daemon_keep_fds.py")
sleep(.1)
def tearDown(self):
os.system("kill `cat %s`" % self.pidfile)
os.remove(self.logfile)
os.remove(self.pidfile)
sleep(.1)
def test_keep_fds(self):
log = open(self.logfile, "r").read()
self.assertEqual(log, "Test\n")
if __name__ == '__main__':
unittest.main()